用SwingWorker打开JDialog?

我有一个使用JPA的项目J2SE。 在一些JDialogs中,我返回getResultList()并在类的构造函数中填充JTable,JComboBox,JList等。

因此,当我为这些对话框创建任何实例时,有时会很慢。

我认为使用SwingWorker和JProgressbar并创建一个(加载)打开JDialogs是一个很好的解决方案,但我不知道该怎么做。

我正在尝试这个。

 // JProgressbar progress = new JProgressBar(); //custommer dialog JDialog custommer = new JDialog(); //here slow because I have List and others lists custommer.setModal(true); private void openDialogs(JDialog dialog){ progress.setVisible(true); progress.setIndeterminate(true); SwingWorker sw = new SwingWorker(){ protected Object doInBackground(){ //opening dialog dialog.setVisible(true); return null; } } //after opened protected void done(){ progress.setVisible(false); } } 

如何打开JDialog并创建一个用SwingWorker和JProgressbar打开的加载?

这里有一个棘手的问题,你需要在显示JDialog之前启动SwingWorker ,但是你还需要在不阻挡代码的其他方面的情况下看到对话框。

此示例使用附加到SwingWorkerPropertyChangeListener来监视STARTED状态,此时它将进度对话框设置为可见,但它使用SwingUtilities.invokeLater这样做,以便不阻止当前事件执行过程……

然后它使用SwingWorkerdone方法在done后关闭对话框…

 import java.awt.EventQueue; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.EmptyBorder; public class LongWaiting { public static void main(String[] args) { new LongWaiting(); } public LongWaiting() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Loading stuff")); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); Loader loader = new Loader(frame); loader.execute(); } }); } public class Loader extends SwingWorker { private ProgressPane progressPane; private JDialog dialog; public Loader(Frame owner) { progressPane = new ProgressPane(); progressPane.setBorder(new EmptyBorder(5, 5, 5, 5)); dialog = new JDialog(owner, "Loading", true); dialog.add(progressPane); dialog.pack(); dialog.setLocationRelativeTo(owner); dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if ("state".equals(evt.getPropertyName())) { if (getState() == StateValue.STARTED) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { if (getState() == StateValue.STARTED) { dialog.setVisible(true); } } }); } } } }); } @Override protected Object doInBackground() throws Exception { Thread.sleep(10000); return null; } @Override protected void done() { dialog.dispose(); } } public class ProgressPane extends JPanel { private JLabel message; private JProgressBar pb; public ProgressPane() { message = new JLabel("Loading..."); pb = new JProgressBar(); pb.setIndeterminate(true); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(2, 0, 2, 0); add(message, gbc); add(pb, gbc); } } }