Java 对话框框架中的JProgressBar工作不正常
我有一个java程序,它加载一个文本文件作为输入,读取其内容,修改一些字符串,然后将结果打印到文本区域。由于此操作需要几秒钟的时间,我希望在此活动期间显示一个JProgressBar,以便通知用户执行正在进行,并且当活动完成时,关闭包含JProgressBar的对话框并打印结果 代码如下:Java 对话框框架中的JProgressBar工作不正常,java,swing,concurrency,jdialog,jprogressbar,Java,Swing,Concurrency,Jdialog,Jprogressbar,我有一个java程序,它加载一个文本文件作为输入,读取其内容,修改一些字符串,然后将结果打印到文本区域。由于此操作需要几秒钟的时间,我希望在此活动期间显示一个JProgressBar,以便通知用户执行正在进行,并且当活动完成时,关闭包含JProgressBar的对话框并打印结果 代码如下: JButton btnCaricaFile = new JButton("Load text file"); panel.add(btnCaricaFile); btnCari
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();
}
}
});
JButton btnCaricaFile=新JButton(“加载文本文件”);
panel.add(BTNCarica文件);
setIcon(UIManager.getIcon(“FileView.directorycon”);
btnCaricaFile.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
//JFileChooser choice=null;
最终JFileChooser选项=新的JFileChooser(userDir+“/Desktop”);
int option=choice.showOpenDialog(GUI.this);
if(option==JFileChooser.APPROVE\u选项){
最终JDialog对话框=新JDialog(GUI.this,“进行中”,true);
JProgressBar progressBar=新的JProgressBar(0,100);
progressBar.SetUndeterminate(真);
dialog.getContentPane().add(BorderLayout.CENTER,progressBar);
dialog.getContentPane().add(BorderLayout.NORTH,新JLabel(“细化字符串…”);
setDefaultCloseOperation(JDialog.DO\u NOTHING\u ON\u CLOSE);
对话框。设置大小(300,75);
setLocationRelativeTo(GUI.this);
线程t=新线程(新的可运行线程(){
公开募捐{
对话框.setVisible(true);
File File=choice.getSelectedFile();
lista.clear();
textArea.setText(“”);
lista=loadFile.openFile(文件);
对于(int i=0;i是的,您正在为文件读取创建一个后台线程,很好,但是您也在同一个后台线程中进行Swing调用,这不太好,这可能会不适当地占用Swing事件线程。关键是要保持线程分离--后台工作在后台线程中进行,而Swing工作在后台线程中进行它只在摆动线中。请阅读更多关于这个的内容
我自己,我会创建并使用一个,并使用worker的将字符串安全地发送到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
final JDialog dialog=new JDialog(GUI.this,“进行中”,true);
JProgressBar progressBar=新的JProgressBar(0,100);
progressBar.SetUndeterminate(真);
dialog.getContentPane().add(BorderLayout.CENTER,progressBar);
dialog.getContentPane().add(BorderLayout.NORTH,新JLabel(“细化字符串…”);
setDefaultCloseOperation(JDialog.DO\u NOTHING\u ON\u CLOSE);
对话框。设置大小(300,75);
setLocationRelativeTo(GUI.this);
lista.clear();
SwingWorker worker=新SwingWorker(){
@凌驾
public Void doInBackground()引发异常{
//全部调用*关闭*事件线程
lista=loadFile.openFile(文件);
对于(int i=0;i
注意:代码未经测试或编译是的,您正在为文件读取创建一个后台线程,很好,但您也在同一个后台线程中进行Swing调用,这不好,而且可能会不适当地占用Swing事件线程。关键是要保持线程分离--后台工作在后台进行和线程,和摆动工作只有在摆动线程。请阅读更多关于这个
我自己,我会创建并使用一个,并使用worker的将字符串安全地发送到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
final JDialog dialog=new JDialog(GUI.this,“进行中”,true);
JProgressBar progressBar=新的JProgressBar(0,100);
progressBar.SetUndeterminate(真);
dialog.getContentPane().add(BorderLayout.CENTER,progressBar);
dialog.getContentPane().add(BorderLayout.NORTH,新JLabel(“细化字符串…”);
setDefaultCloseOperation(JDialog.DO\u NOTHING\u ON\u CLOSE);
对话框。设置大小(300,75);
setLocationRelativeTo(GUI.this);
lista.clear();
SwingWorker worker=新SwingWorker(){
@凌驾
public Void doInBackground()引发异常{
//全部调用*关闭*事件线程
lista=loadFile.openFile(文件);
对于(int i=0;i
注意:未测试或未编译的代码请查看要回答的编辑并询问是否有任何问题请查看要回答的编辑并询问是否有任何问题