简体   繁体   English

JDialog的模式时,JProgressBar不会更新

[英]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 . 我了解没有办法制作JProgressMonitor模态,而宁愿使用带有JProgressBarJDialog Now, I got that working - but only as long as I'm not trying to make the JDialog modal. 现在,我可以正常工作了-但是只要不尝试使JDialog模式化即可。 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(); 调用createJDialog(); after thread start not from inner Runnable . 线程启动后,不是从内部Runnable启动。

The call to d.setVisible(true) blocks until the dialog is closed when the dialog is modal, per the Java API docs . 根据Java API文档 ,对d.setVisible(true)的调用将d.setVisible(true)阻塞,直到对话框处于模式状态时对话框关闭为止。

Try kicking off that call in a separate thread. 尝试在一个单独的线程中启动该调用。

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

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