对话框中的JProgressBar无法正常工作

时间:2018-05-16 15:24:07

标签: java swing concurrency jdialog jprogressbar

我有一个java程序,它将文本文件作为输入加载,读取其内容,修改一些字符串,然后将结果打印到textarea。由于此操作需要几秒钟,我想在此活动期间显示JProgressBar,以便通知用户执行正在进行以及活动何时完成,关闭包含JprogressBar的对话框并打印结果。

以下是代码:

JButton btnCaricaFile = new JButton("Load text file");
        panel.add(btnCaricaFile);
        btnCaricaFile.setIcon(UIManager.getIcon("FileView.directoryIcon"));
        btnCaricaFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //JFileChooser choice = null;
                final JFileChooser choice = new JFileChooser(userDir +"/Desktop");
                int option = choice.showOpenDialog(GUI.this);
                if (option == JFileChooser.APPROVE_OPTION) {
                    final JDialog dialog = new JDialog(GUI.this, "In progress", true);
                    JProgressBar progressBar = new JProgressBar(0, 100);
                    progressBar.setIndeterminate(true);
                    dialog.getContentPane().add(BorderLayout.CENTER, progressBar);
                    dialog.getContentPane().add(BorderLayout.NORTH, new JLabel("Elaborating strings..."));
                    dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                    dialog.setSize(300, 75);
                    dialog.setLocationRelativeTo(GUI.this);
                    Thread t = new Thread(new Runnable() {
                        public void run() {
                            dialog.setVisible(true);
                            File file = choice.getSelectedFile();
                            lista.clear();
                            textArea.setText("");
                            lista = loadFile.openFile(file);
                            for(int i=0; i<lista.size(); i++) {
                                textArea.append(lista.get(i)+"\n");
                            }
                            dialog.setVisible(false);
                        }
                    });
                    t.start();
                }
            }
        });

为此目的,我使用JDialog作为由相应线程执行的JProgressBar的容器。问题是进度条显示无限时间,并且不会在textarea中打印任何内容。

你可以帮我解决这个问题吗? 感谢

1 个答案:

答案 0 :(得分:4)

是的,你正在为你的文件读取创建一个后台线程,好,但是你也在同一个后台线程中进行Swing调用,这不太好,这很可能会不合适地占用Swing事件线程。关键是要保持线程分离 - 后台工作在后台线程中进行,Swing工作仅在Swing线程中进行。请详细阅读Lesson: Concurrency in Swing

我自己,我会创建并使用SwingWorker<Void, String>,并使用工作人员的publish/process method pair安全地将字符串发送到JTextArea。

例如,像......

final JDialog dialog = new JDialog(GUI.this, "In progress", true);
JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setIndeterminate(true);
dialog.getContentPane().add(BorderLayout.CENTER, progressBar);
dialog.getContentPane().add(BorderLayout.NORTH, new JLabel("Elaborating strings..."));
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.setSize(300, 75);
dialog.setLocationRelativeTo(GUI.this);
lista.clear();
SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {

    @Override
    public Void doInBackground() throws Exception {
        // all called *off* the event thread
        lista = loadFile.openFile(file);
        for (int i = 0; i < lista.size(); i++) {
            publish(lista.get(i));
        }
        return null;
    }

    @Override
    protected void process(List<String> chunks) {
        // called on the event thread
        for (String chunk : chunks) {
            textArea.append(chunk + "\n");
        }
    }

    // called on the event thread
    public void done() {
        dialog.setVisible(false);
        // should call get() here to catch and handle
        // any exceptions that the worker might have thrown
    }
};
worker.execute();
dialog.setVisible(true); // call this last since dialog is modal

注意:未经测试或编译的代码

相关问题