Progress bar for loading out another JFrame class

独自空忆成欢 提交于 2020-01-05 05:26:14

问题


I have 2 JFrame: NewJFrame and NewJFrame1. NewJFrame has a progress bar and a button. the button is used to call up NewJFrame1. When the button is clicked it should trigger the progress bar to run until NewJFrame1 pop up. However, how do I make use of the swing worker and let the progress to run until NewJFrame1 actually competely loaded all of its component?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    SwingUtilities.invokeLater(new Runnable() {
        int i = 0;
        public void run() {
           jProgressBar1.setValue(i++);
           new NewJFrame1().setVisible(true);
        }
    });
} 

回答1:


Let your top-level container have a SwingWorker that loads it's content, as shown here, and a PropertyChangeListener, as shown here. When doInBackground() calls setProgress(), the listener will see the event.

Addendum: How do we know how much time left for the GUI to be completely loaded? …in examples, the progress bar is run with random number as simulated latency.

Correct; the simulated latency represents the granularity of your loading. Displaying the fraction of total bytes loaded would be ideal, but you may have to settle for a coarser frequency. Note that models may be constructed in the background, before any components are listening. GUI components should be constructed and manipulated only on the EDT, possibly in the worker's process() implementation.



来源:https://stackoverflow.com/questions/20443437/progress-bar-for-loading-out-another-jframe-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!