2013-05-20 33 views
0

Find()方法是我的方法的一个示例,它将单词搜索到一些文件中。 我从Button_Start鼠标点击事件中调用它。
这里是我的代码:java - JProgressBar方法运行时

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 
); 
} 

我需要添加两件事情:

  1. 我想打一个JProgressBarFind()方法运行。
    在这种情况下,我需要采用一个标准来设置设置栏进度 即基于文件列表长度,我不知道。
  2. 我想设置WAIT_CURSORFind()方法正在运行,所以直到 进度条达到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()。 问题是我不知道如何同步我的方法持续时间和进度条长度。 除此之外,我不知道这是否是达到我的目标的最佳方式。

你能帮我举一个例子吗?根据我的代码/情况?

感谢

编辑:这里是我的目标:我的方法在JTextPanetextpane.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 
+1

请不要攻击,不要undestand,也许代码和描述不一样,请问whats goal,也许JTextPane,可能是Jlist或者JTable,或者???,也许我错了,并且错过了一些东西 – mKorbel

+1

为了更好的帮助更快,发布[SSCCE](http://sscce.org/)。 –

回答

2

什么publish()?我不明白我必须做什么。

方法publish()process()SwingWorker可靠地从后台线程通信interim results事件调度线程(EDT)。在此example中,process()显示长期运行计算的当前结果;在这个相关的example,process()也更新了图表的数据模型。

附录:正如@mKorbel所说,您可以通过使用更具体的类型(例如, SwingWorker<Double, Double>

+1

+1,但不确定是否声明SwingWorker 是适当的方式,仍然缺少这个问题的原因,(留给鲸鱼) – mKorbel

2

我认为你需要通过查找()进入的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())); 
    } 
} 
+0

对不起,什么是“发布”?我不明白我必须做什么 – Frank

+0

publish是SwingWorker上的受保护方法...但在这种情况下,您实际上想要使用setProgress,另一个受保护的SwingWorker方法。我已经更新了我的答案。你的听众现在应该选择这些进度事件。 –

+0

用于'setProgress()'的+1,可以在后台线程调用;我详细阐述了'publish' [here](http://stackoverflow.com/a/16652962/230513)。 – trashgod