JList/DefaultListModel持续Insert和Remove数据产生ArrayIndexOutOfBoundsException的问题
来源:互联网 发布:u盘进水数据恢复 编辑:程序博客网 时间:2024/05/02 00:01
有这样一个需求:往一个JList中持续Insert数据,当JList中的数据量大于某个值时(比如大于5行),就开始删除数据,使得JList中的数据量一直为固定值(如5行),下面是测试代码:
上面的测试代码会每隔一段时间就报一个异常:
经过搜索查找,终于找到问题所在:原来并不是DefaultListModel边界的溢出,而是上面插入/删除数据是在一个新的线程里进行,而界面绘制要求在Event Dispatch Thread中进行,所以当应用程序线程需要更新GUI时,需要把代码包在SwingUtilities.invokeLater()中来处理,使得它们重新回到Event Dispatch Thread中。
看看invokeLater的描述:
public static void invokeLater(Runnable doRun)
Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.
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.");
把上面测试例子稍微改一下:
另外,ArrayIndexOutOfBoundsException也可能是由于数组边界溢出,如果是数组边界溢出,提示应该这个样子的:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
参考:http://www.mofeel.net/858-comp-lang-java-gui/5523.aspx
- JList/DefaultListModel持续Insert和Remove数据产生ArrayIndexOutOfBoundsException的问题
- JList/DefaultListModel持续Insert和Remove数据产生ArrayIndexOutOfBoundsException的问题
- JList.removeAll()与((DefaultListModel)JList.getModel).removeAllElements()
- swing JList JScrollPane使用时不显示数据的问题 JList设置可多选
- swing JList JScrollPane使用时不显示数据的问题 JList设置可多选
- JList的问题
- [经验小结]jxl写excel时产生“java.lang.ArrayIndexOutOfBoundsException: 5”问题的解决办法
- Swing 有关JList的问题
- jcombox和jlist的区别
- direct insert下不会产生数据的 UNDO
- 随机数据产生的问题
- C++ list中insert、erase和remove函数的用法
- 对比insert和delete操作产生的undo
- Swing中JList的相关问题(2)
- swing 中的JList的增加和删除
- Java JList的使用和事件响应
- Swing组件JList的列表数据修改了,如何通知JList改变显
- 游标类型产生的数据检索问题
- 用组策略实现用户证书的自动注册申请:ISA2006系列之二十八
- 详解DMZ的部署与配置:ISA2006系列之二十九
- 企业根CA方法客户机证书的解决方案,ISA2006系列之三十
- 有关Hibernate优点和缺点的阐述
- 删除列表框选择的行
- JList/DefaultListModel持续Insert和Remove数据产生ArrayIndexOutOfBoundsException的问题
- MySQL安装 Apply security settings报错1045解决方案
- 爱情就像一场DOTA,可惜.女生不懂DOTA
- AT指令全集
- 谈谈大型高并发高负载网站的系统架构
- nat
- samba入门指导
- RSS in ASP 1
- RSS in ASP 2