简体   繁体   中英

JProgressBar doesn't update when JDialog's modal

I understand there's no way to make a JProgressMonitor modal and that one would rather use a JDialog with a JProgressBar . Now, I got that working - but only as long as I'm not trying to make the JDialog modal. Can anyone tell me what I'm doing wrong?

private Frame frame;
private JPanel contentPane;
private JProgressBar progressBar;

public MainClass() {
    JButton startBtn = new JButton("Start");
    startBtn.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(final ActionEvent arg0)
        {
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    createJDialog();

                    for (int i = 0; i < 100; ++i)
                    {
                        final int j = i;
                        doInBackground(); // Batch process

                        SwingUtilities.invokeLater(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                progressBar.setValue(j);
                            }
                        });
                    }
                }
            }).start();
        }
    });
}

public void createJDialog()
{
    JDialog d = new JDialog(); 
    d.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    // Keeps progressBar from updating
    // d.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
    // d.setModal(true);
    d.getContentPane().add(progressBar, BorderLayout.PAGE_START); 
    d.getContentPane().add(progressBar, BorderLayout.PAGE_END); 
    d.pack(); 
    d.setVisible(true); 
}

Call createJDialog(); after thread start not from inner Runnable .

The call to d.setVisible(true) blocks until the dialog is closed when the dialog is modal, per the Java API docs .

Try kicking off that call in a separate thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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