DefaultList 与 ArrayOutOfBoundsException
2010-11-11 00:18
375 查看
备忘:
一个JFrame用到了 DefaultListModel,代码主要如下:
class MyFrame extends JFrame {
DefaultListModel model = new DefaultListModel();
......
}
在另一个线程中对此model持续进行添加和删除元素的操作:
{
model.add(element);
model.remove(element);
N次....
}
一段时间后会出现 ArrayOutOfBoundsException异常.
经测试,此处应该不是数组越界导致的异常。
查资料后才知道,应改为如下:
SwingUtilities.invokeLater (new Runnable() {
public void run() {
model.add(element);
model.remove(element);
}
});
导致 doRun.run() 在 AWT 事件指派线程上异步执行。在所有挂起的 AWT 事件被处理后才发生。此方法应该在应用程序线程需要更新该 GUI 时使用。在下面的示例中,
如果从事件指派线程调用 invokeLater(例如,从 JButton 的 ActionListener)则仍要将 doRun.run() 推迟,直到处理了所有挂起的事件。注意,如果 doRun.run() 抛出一个未捕获的异常,则该事件指派线程将不会缠绕(不是该当前线程)。
有关此方法的其他文档和示例,请参阅 The Java Tutorial 中的 How to Use Threads 一节。
从 1.3 版本开始,此方法只覆盖了
与 Swing 的其余部分不同,可从任一线程中调用此方法。
N次....
一个JFrame用到了 DefaultListModel,代码主要如下:
class MyFrame extends JFrame {
DefaultListModel model = new DefaultListModel();
......
}
在另一个线程中对此model持续进行添加和删除元素的操作:
{
model.add(element);
model.remove(element);
N次....
}
一段时间后会出现 ArrayOutOfBoundsException异常.
经测试,此处应该不是数组越界导致的异常。
查资料后才知道,应改为如下:
SwingUtilities.invokeLater (new Runnable() {
public void run() {
model.add(element);
model.remove(element);
}
});
invokeLater (JDK1.6.0)
public static void invokeLater(Runnable doRun)
导致 doRun.run() 在 AWT 事件指派线程上异步执行。在所有挂起的 AWT 事件被处理后才发生。此方法应该在应用程序线程需要更新该 GUI 时使用。在下面的示例中,
invokeLater调用将事件指派线程上的
Runnable对象
doHelloWorld加入队列,然后输出一条信息。
Runnable doHelloWorld = new Runnable() { public void run() { System.out.println("Hello World on " + Thread.currentThread()); } }; SwingUtilities.invokeLater(doHelloWorld); System.out.println("This might well be displayed before the other message.");
如果从事件指派线程调用 invokeLater(例如,从 JButton 的 ActionListener)则仍要将 doRun.run() 推迟,直到处理了所有挂起的事件。注意,如果 doRun.run() 抛出一个未捕获的异常,则该事件指派线程将不会缠绕(不是该当前线程)。
有关此方法的其他文档和示例,请参阅 The Java Tutorial 中的 How to Use Threads 一节。
从 1.3 版本开始,此方法只覆盖了
java.awt.EventQueue.invokeLater()。
与 Swing 的其余部分不同,可从任一线程中调用此方法。
N次....
相关文章推荐
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- Android常见出错:OutOfMemoryError与ArrayIndexOutOfBoundsException
- 合并图片 java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- Hibernate query.list()查询发生java.lang.StringIndexOutOfBoundsException: String index out of range: 0错误
- list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException: String index out of range: -1
- java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 21
- Hibernate query.list()查询发生java.lang.StringIndexOutOfBoundsException: String index out of range: 0错误
- spark将数据保存到elasticsearch,老是爆java.lang.StringIndexOutOfBoundsException: String index out of range: -1
- java.lang.StringIndexOutOfBoundsException: String index out of range: 1这是怎么回事?
- java.lang.ArrayIndexOutofBoundException Array index out of range 0
- java.lang.StringIndexOutOfBoundsException: String index out of range: 0
- NoteExpress:List index out of bounds
- 【解决问题.思路篇】StringIndexOutOfBoundsException:String index out of range: -1
- 关于List index out of bounds(0)的问题
- 关于使用自定义Adapter,多种布局出现ArrayIndexOutOfException
- mvn install:java.lang.StringIndexOutOfBoundsException: String index out of range: -1问题的解决
- 报错 MoveTargetOutOfBoundsException 的解决办法
- 严重: Servlet.service() for servlet default threw exception java.lang.OutOfMemoryError: Java heap spac
- Exception while getting block list java.lang.ArrayIndexOutOfBoundsException