[英]java - JProgressBar while method is running
Find()
方法是我的方法的样本,该方法将单词搜索到某些文件中。 我从Button_Start
mouseclicked事件中调用它。
这是我的代码:
public void Find (String word) {
List_files_directories (directory.getAbsolutePath(), files);
// print list
textpane.append(java.awt.Color.cyan, "Files in the choosen directory\n");
for (int j=0; j<files.size(); j++) {
if (files.get(j) != null)
textpane.append( java.awt.Color.PINK, "\n" + files.get(j) );
}
// from listarray to array
String[] array_files = new String[files.size()];
array_files = files.toArray(array_files);
int j;
for (j=0; j<array_files.length; j++) {
if (array_files[j] != null) {
if (array_files[j].contains("1")) {
function1 ( array_files[j], word );
}
else if ( array_files[j].contains("2") )
{
function2 ( array_files[j], word);
}
else if ( array_files[j].contains("3") )
{
function3 ( array_CARTELLE[j], word );
}
}
}
}
private void Button_StartMouseClicked(java.awt.event.MouseEvent evt) {
java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
Find(word_from_textfield);
}
},
10
);
}
我需要添加2件事:
Find()
方法运行时制作一个JProgressBar
。 Find()
方法时设置WAIT_CURSOR
,所以直到进度条达到100%。 我为我的要求尝试了1):
public void doWork() {
Worker worker = new Worker();
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
ProgressBar1.setValue((Integer) evt.getNewValue());
}
}
});
worker.execute();
}
public class Worker extends SwingWorker<Object, Object> {
@Override
protected Object doInBackground() throws Exception {
for (int index = 1; index <= 1000; index++) {
int progress = Math.round(((float) index / 1000) * 100f);
setProgress(progress);
Thread.sleep(100);
}
return null;
}
}
在此方法中,我在Find()
方法的开头调用doWork()
。 问题是我不知道如何同步我的方法持续时间和进度条长度。 此外,我不知道这是否是实现我目标的最佳方法。
您能帮我根据我的代码/情况举一个例子吗?
谢谢
编辑 :这是我的目标:我的方法在JTextPane
( textpane.append()
)上打印用户目录中的所有文件,然后在每个文件中搜索一个单词。 我想在JTextPane上打印时使用JProgressBar,它必须与过程持续时间同步。 如果可能,则必须定时打印: 示例 :
// Set Mouse cursor to Wait Status
// execute Find():
// the JProgressBar has to follow this progress of the print on JTextPane.
Files in the choosen directory:
file1
// print on JTextPane after 1 second
file2
// print on JTextPane after 1 second
file3
// print on JTextPane after 1 second
word found in file1!
// print on JTextPane after 1 second
word not found in file2
// print on JTextPane after 1 second
word found in file3
// print on JTextPane after 1 second
// restore mouse cursor to default
我认为您需要将Find()完成的工作集成到SwingWorker中。 然后,您可以在处理每个文件后设置进度栏。
@Override
protected Object doInBackground() throws Exception {
for (int j=0; j<files.size(); j++) {
if (files.get(j) != null)
textpane.append( java.awt.Color.PINK, "\n" + files.get(j) );
}
// from listarray to array
String[] array_files = new String[files.size()];
array_files = files.toArray(array_files);
int j;
for (j=0; j<array_files.length; j++) {
if (array_files[j] != null) {
if (array_files[j].contains("1")) {
function1 ( array_files[j], word );
}
else if ( array_files[j].contains("2") )
{
function2 ( array_files[j], word);
}
else if ( array_files[j].contains("3") )
{
function3 ( array_CARTELLE[j], word );
}
// THIS IS THE NEW BIT
setProgress((int)(100 * (float) j / files.size()));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.