Progress bar for loading out another JFrame class

2019-08-05 19:03发布

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条回答
相关推荐>>
2楼-- · 2019-08-05 19:10

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.

查看更多
登录 后发表回答