在 Swing Java 中向 JList 添加元素
我有一个在单击按钮时执行的函数。假设有一个循环将 1 到 10 添加到 JList
。我将该数据添加到 DefaultListModel
中。它工作完美并且数字被添加。然后我在循环中添加了一个 Thread.sleep(1000)
。但输出不同。我想每秒添加 1 个元素。但现在它等待 10 秒,并在第 10 秒结束时将所有 1 与 10 加在一起。我有什么地方说错了吗?
List processList = listNumbers.getSelectedValuesList();
DefaultListModel resultList = new DefaultListModel();
listResult.setModel(resultList);
for (int i = 0; i < processList.size(); i++) {
resultList.addElement(String.valueOf(i));
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
}
}
I have a function that executes when a button is clicked. Suppose there is a loop to add 1 to 10 to a JList
. I add that data to DefaultListModel
. It works perfectly and the numbers get added. Then I added a Thread.sleep(1000)
within the loop. But the output is different. I wanted to add 1 element every second. But now it waits for 10secs and the add all 1 to 10 together at the end of 10th second. Am I wrong anywhere?
List processList = listNumbers.getSelectedValuesList();
DefaultListModel resultList = new DefaultListModel();
listResult.setModel(resultList);
for (int i = 0; i < processList.size(); i++) {
resultList.addElement(String.valueOf(i));
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用,真的不要在
EventDispashThread
期间使用Thread.sleep(int)
,因为 sleep 锁定了当前Thread
,在这种情况下 < code>EventDispashThread,与 GUI 的意外输出,更多信息请参见 Swing 中的并发性,如果您需要一些东西来延迟,然后将 Itemswrappend 添加到 Runneble#Thread 中,并将 GUI 的输出包装到
invokeLater
中,或者最好的方法是使用 javax.swing.Timer编辑第一。示例如何使用 Thread.sleep(int) 以编程方式阻止 EDT,因为否则不适用于预期到 GUI 的输出(代码非常慢,因为我想编码)
编辑第二。 @camickr 的友善帮助(看起来像类似)
编辑第三。
但对于所有示例,必须存在 EDT,并且在
EventQueue
中包含Events
,否则简单的方法不起作用don't use, really don't use
Thread.sleep(int)
duringEventDispashThread
, because sleep locked currentThread
and in this caseEventDispashThread
, with un-expection output to the GUI, more in Concurency in Swing,if you needed something to dealay, then add Items wrappend into
Runneble#Thread
, with output to the GUI wrapped intoinvokeLater
, or the best way is using javax.swing.TimerEDIT 1st. example how to programatically block EDT with Thread.sleep(int), because othewise doesn't works with expecting output to the GUI (code is very loonnnger as I want to code)
EDIT 2nd. with kind hepl by @camickr (look like similair)
EDIT 3rd.
but for all examples there must EDT exist and with
Events
in theEventQueue
otherwise simple doesn't works您应该在单独的线程中更新列表,否则最终会阻塞事件调度线程。
请尝试以下操作:
You should update your list in a separate thread otherwise you end up blocking the event dispatch thread.
Try the following:
此代码解释了如何从 java 中的结果集向 Swing JList 中插入值。根据您的要求进行更改。希望这会对您有所帮助。
或者只是简单的代码来插入值
});
请记住将此代码添加到页面顶部的 initComponents() 之后。
This code explains how to insert values in Swing JList from a resultset in java. Make changes as per your requirements. hope this will help you.
Or Just Simple Code to Insert values
});
Remember to add this code in after initComponents() at the top of the page.