gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:28:26 25 4
gpt4 key购买 nike

我有一个 java 程序,它加载一个文本文件作为输入,读取它的内容,修改一些字符串,然后将结果打印到文本区域。由于此操作需要几秒钟,我想在此 Activity 期间显示 JProgressBar,以便通知用户执行正在进行中,并在 Activity 完成后关闭包含 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 的容器。问题是进度条会无限期显示,并且不会在文本区域打印任何内容。

你能帮我解决这个问题吗?谢谢

最佳答案

是的,您正在为您的文件读取创建一个后台线程,这很好,但是您也在同一个后台线程中进行 Swing 调用,这并不好,而且这可能不适本地占用了 Swing 事件线程。关键是要使您的线程分离——后台工作在后台线程中进行,而 Swing 工作仅在 Swing 线程中进行。请阅读Lesson: Concurrency in Swing更多关于这一点。

我自己,我会创建并使用一个 SwingWorker<Void, String> ,并使用 worker 的 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

注意:代码未经测试和编译

关于java - 对话框中的 JProgressBar 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50374742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com