每次通过循环向 jTable 添加一行?

发布于 2024-08-23 02:54:41 字数 347 浏览 3 评论 0原文

我有一个 jTable 和一个 jButton。单击时,按钮的 actionPerformed 方法会调用另一个包含 while 循环的方法,并在每次通过循环时向表模型 (DefaultTableModel) 添加一行。 while 循环可以运行几分钟,所以我希望它在 GUI 中显示每次添加到表中的行,一一显示。然而现在,它在循环完成后将所有行添加到表中,因此不是在几分钟内逐一增加行数,而是从显示 0 行表的几分钟变为然后立即就有数千。我尝试过在桌子上调用 updateUI、repaint 等,以及在模型上调用 fireTabledDataChanged 等,但这些都没有任何区别。我也尝试过使用 Swing Timer,但没有成功。我非常感谢您提供的任何帮助或指导,谢谢。

I have a jTable and a jButton. When clicked, the button's actionPerformed method calls another method that contains a while loop and adds a row to the table's model (DefaultTableModel) each time though the loop. The while loop can run for a few minutes so I want it to show in the GUI the rows being added to the table each time, one by one. However right now it adds all the rows to the table together after the loop is finished, so instead of incrementing the number of rows one by one over the course of a few minutes it goes from a few minutes of showing a table with 0 rows to then instantly having thousands. I've tried calling updateUI, repaint etc on the table as well as calling fireTabledDataChanged etc on the model but none of this made any difference. I've also tried using a Swing Timer but to no avail. I'd appreciate any help or guidance offered, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2024-08-30 02:54:41

阅读 Swing 教程中关于 并发 的部分,其中解释有关 EDT 如何工作的更多详细信息。除了创建自己的线程并使用 SwingUtilties.invokeLater() 之外,您还可以使用更新的 SwingWorker 方法。本教程包含一个示例。

我尝试过调用 updateUI,

但永远不要做类似的事情。即使它解决了您的问题,也是错误的解决方案。您不是在更新 UI,而是在更新组件。

Read the section from the Swing tutorial on Concurrency which explains in more detail about how the EDT works. In addition to the create your own Thread and use SwingUtilties.invokeLater(), you can also use the newer of approach of using a SwingWorker. The tutorial contains an example.

I've tried calling updateUI,

Never do something like that. Even if it fixes your problem, it is the wrong solution. You are not updating the UI, you are updating a component.

辞慾 2024-08-30 02:54:41

如果您使用的是 DefaultTableModel,则在每次迭代期间对模型调用 addRow 应该更新模型,然后模型又应该更新 JTable。该表将请求重新绘制,该请求将在 EDT 上进行。不幸的是,您的长时间运行的进程阻碍了 EDT 上处理其他请求。对您来说最好的做法是让按钮触发工作线程中的进程,然后该线程可以在 Runnable 中执行其 addRow 调用,并将其拖放到通过 SwingUtilities.invokeLater 的 EDT

If you are using a DefaultTableModel, calling addRow on your model during each iteration should update the model, which should then in turn update the JTable. The table will request that it be repainted, and that request will go on the EDT. Unfortunately, your long running process is holding up other requests from getting handled on the EDT. The best thing for you to do is have your button trigger a process in a worker thread, and that thread could then have its addRow calls performed in a Runnable that gets dropped onto the EDT via SwingUtilities.invokeLater

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文