【问题标题】:JProgressBar doesn't update when JDialog's modalJDialog的模态时JProgressBar不更新
【发布时间】:2013-01-09 16:38:46
【问题描述】:

我知道没有办法制作 JProgressMonitor 模态,人们宁愿使用 JDialogJProgressBar。现在,我开始工作了——但前提是我不尝试制作 JDialog 模态。谁能告诉我我做错了什么?

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); 
}

【问题讨论】:

    标签: java swing modal-dialog jdialog jprogressbar


    【解决方案1】:

    在线程启动后调用createJDialog();,而不是从内部Runnable

    【讨论】:

    • 斯坦尼斯拉夫,你成就了我的一天!非常感谢!
    【解决方案2】:

    根据Java API docs,当对话框处于模态时,对d.setVisible(true) 的调用会阻塞直到对话框关闭。

    尝试在单独的线程中启动该调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      相关资源
      最近更新 更多