简体   繁体   English

Swing JTable更新

[英]Swing JTable update

Am working on a project in swings and in that am adding rows to a JTable in a while loop. 我正在使用swing进行项目,并在while循环中向JTable添加行。

My Scenario is this :- 我的情景是这样的: -

As soon as the user clicks a button the program enters a while() loop and start adding rows to the DefaultTableModel of the Jtable one by one till the while loop terminates. 一旦用户单击按钮,程序就会进入while()循环并开始逐行向Jtable的DefaultTableModel添加行,直到while循环终止。 But the thing is that the table gets updated with the data only after the while loop has ended.I want it to update after adding each row and show it on the UI. 但问题是,只有在while循环结束后才能使用数据更新表。我想在添加每一行后更新并在UI上显示它。

It would be really nice if someone could help me out with this provide a solution 如果有人能帮助我提供解决方案,那将是非常好的

I have already tried repaint() after adding each row but it didn't work. 我已经在添加每一行后尝试了repaint()但它没有用。

You need to run your operation in a seperate thread and then update the JTable in the gui thread. 您需要在单独的线程中运行您的操作,然后在gui线程中更新JTable。 Something like this: 像这样的东西:

public void someButtonClicked(params...) {
    new Thread(new Runnable() {
        public void run() {
            longOperation();
        }
    }).start();
}

public void longOperation() {
    for(int i=0; i<1000; i++) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // add new row to jtable
            }
        });
    }
}

i think you should go for frequently updating the row. 我认为你应该经常更新这一行。 There is a tutorial given by sun called the "Christmas tree". 太阳给出了一个名为“圣诞树”的教程。 Here is link for that 这是链接

http://java.sun.com/products/jfc/tsc/articles/ChristmasTree/ http://java.sun.com/products/jfc/tsc/articles/ChristmasTree/

Above link will help you for frequently update rows in jTable. 以上链接将帮助您频繁更新jTable中的行。

If your entire while loop is running on the Swing event dispatch thread in response to the button click event, the thread will not be free to update the view on the screen until your event-handling code completes (the end of your actionPerformed method). 如果整个while循环在Swing事件调度线程上运行以响应按钮单击事件,则线程将无法自由更新屏幕上的视图,直到事件处理代码完成(actionPerformed方法结束)。

I'm not sure what you're trying to accomplish here -- animation perhaps? 我不确定你在这里想要完成什么 - 动画也许? You could use Swing's Timer class (javax.swing.Timer) to fire an event repeatedly with a small delay between firings, and in response to each event you could add a row to the table. 您可以使用Swing的Timer类(javax.swing.Timer)重复触发事件,在触发之间有一个小延迟,并且为了响应每个事件,您可以向表中添加一行。 As long as your event-handling code exits quickly in response to each event fired, Swing should be able to redraw the view in between events. 只要您的事件处理代码快速退出以响应每个事件触发,Swing就应该能够在事件之间重绘视图。 You really need to understand Swing's threading model pretty well or these types of problems are really confusing. 你真的需要很好地理解Swing的线程模型,或者这些类型的问题确实令人困惑。 It's not too difficult -- there are good resources out there to read if you search for eg "Swing event thread." 这并不太难 - 如果你搜索例如“Swing event thread”,那里有很好的资源可供阅读。

Re: calling repaint() -- this won't work as repaint() and the other methods like validate() etc. will only ever flag a component as needing to be repainted -- the component won't actually be repainted on screen until Swing gets a chance to do this, and if you are trapping the Swing thread in a while loop, it won't be free to do the painting until you end your loop and your event handling code finishes. Re:调用repaint() - 这不会像repaint()一样工作,而其他方法如validate()等只会将组件标记为需要重新绘制 - 组件实际上不会在屏幕上重新绘制直到Swing有机会这样做,并且如果你在一个while循环中捕获Swing线程,那么在你结束循环并且事件处理代码完成之前,它将无法自由地进行绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM