[英]Set Progressbar from another class
I'm trying to set my progressbar from another class but failed. 我正在尝试从另一个类设置我的进度条但是失败了。 This is my progressbar the class Upload
. 这是我的进度栏Upload
类。
JProgressBar progressBar = new JProgressBar();
progressBar.setBounds(91, 134, 284, 17);
frame.getContentPane().add(progressBar);
I have another class called Read
. 我有另一个名为Read
类。 In this class I have a loop that needs to read a file, so I want to set the progress with the iterator i
from that class. 在这个类中,我有一个需要读取文件的循环,所以我想用该类的迭代器i
设置进度。 I know that I have to set the minimum and maxium like this: 我知道我必须像这样设置最小值和最大值:
progressBar.setMinimum(0);
progressBar.setMaximum(numRows);
And for the value: 并为价值:
progressBar.setValue(newValue);
How can I set the value for the progressbar so it keeps updating the value of i
? 如何设置进度条的值,以便不断更新i
的值?
First of all, your Reader class must have a reference to the component (JProgressBar) in question. 首先,您的Reader类必须引用相关组件(JProgressBar)。 I understand that this is already solved. 我知道这已经解决了。
After that, for reading a file I strongly recommend to use a class extending javax.swing.SwingWorker
. 之后,为了阅读文件,我强烈建议使用扩展javax.swing.SwingWorker
的类。 That pattern allows you to work in background (may be for heavy operations like reading big files) without affecting the AWT thread, so your GUI won't be affected. 该模式允许您在后台工作(可能用于重读操作,如读取大文件)而不影响AWT线程,因此您的GUI不会受到影响。 So, your reader should extend javax.swing.SwingWorker
. 因此,您的读者应该扩展javax.swing.SwingWorker
。
As a brief resume, it provides a method called doInBackground
where you should implement the 'heavy' operation and also a method called process
where it is received the data chunks and you add your own calculations for the progress. 作为一个简短的简历,它提供了一个名为doInBackground
的方法,您应该在其中实现“重”操作以及一个名为process
,在该方法中接收数据块并为进度添加自己的计算。
The process
method is executed in AWT, so you can update any UI Component at this method (so a good point to update your progress bar) process
方法在AWT中执行,因此您可以使用此方法更新任何UI组件(因此更新进度条的好处)
Please, refer to this link for more information. 请参阅此链接以获取更多信息。
You should use SwingWorker for it as described in the JProgressBar documentation . 您应该按照JProgressBar文档中的描述使用SwingWorker。
This SwingWorker is a thread on which we can add a propertyChangeListener 这个SwingWorker是一个我们可以在其上添加propertyChangeListener的线程
After which we can call firePropertyChange to invoke the listener. 之后我们可以调用firePropertyChange来调用监听器。 In the listener we can update the progress bar by calling setValue() on it. 在监听器中,我们可以通过调用setValue()来更新进度条。
Following is a working example of reading a file and updating the progress bar. 以下是读取文件和更新进度条的工作示例。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class ProgressBar {
private static final String PROGRESS_PROPERTY_NAME = "progress";
final static String fileName = "My_file_location";
public static void main(String[] args) {
JFrame frame = new JFrame("Progress bar");
JPanel panel = new JPanel();
final JProgressBar progressBar = new JProgressBar();
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setStringPainted(true);
panel.add(progressBar);
JButton button = new JButton("Start reading");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ReadFiletask task = new ReadFiletask(fileName);
task.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (PROGRESS_PROPERTY_NAME.equals(evt.getPropertyName())) {
progressBar.setValue((Integer)evt.getNewValue());
}
}
});
task.execute();
}
});
panel.add(button);
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static class ReadFiletask extends SwingWorker<Boolean, Integer> {
String fileName;
public ReadFiletask (String fileName) {
this.fileName = fileName;
}
@Override
protected Boolean doInBackground() throws Exception {
System.out.println("Started reading");
byte[] buffer = new byte[1024 * 1024];//1mb
InputStream is = null;
try {
Path file = Paths.get(fileName);
long fileSize = Files.size(file);
is = Files.newInputStream(file, StandardOpenOption.READ);
int readLen = -1;
int readCounter = 0;
while((readLen = is.read(buffer)) != -1) {
readCounter += readLen;
int percentDone = (int)( (readCounter * 100.0) / fileSize);
System.out.println("done : " + percentDone);
/*Most important part is here */
firePropertyChange(PROGRESS_PROPERTY_NAME, 0, percentDone);
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(is != null) {
try {
is.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
System.out.println("Finished");
return true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.