【问题标题】:java - JProgressBar while method is runningjava - 方法运行时的 JProgressBar
【发布时间】:2013-05-20 12:57:19
【问题描述】:

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

我需要添加两件事:

  1. 我想在 Find() 方法运行时创建一个 JProgressBar
    在这种情况下我需要采用一个标准来设置设置进度条 即基于文件列表的长度,我不知道。
  2. 我想在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

【问题讨论】:

  • 请不要攻击,不要理解,可能代码与描述不同,请问目标是什么,可能是 JTextPane,可能是 Jlist 或 JTable,或者???,也许我错了,看错了某事
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing sync swingworker jprogressbar


【解决方案1】:

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

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

附录:正如@mKorbel 所说,您可以通过使用更具体的类型来利用泛型类型检查,例如SwingWorker&lt;Double, Double&gt;.

【讨论】:

  • +1 但不确定 declare SwingWorker 是否正确,仍然缺少这个问题的原因,(留给鲸鱼)
【解决方案2】:

我认为您需要将 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()));
    }
} 

【讨论】:

  • 抱歉,什么是“发布”?我不明白我必须做什么
  • publish 是 SwingWorker 上的受保护方法...但在这种情况下,您实际上想要使用 setProgress,这是另一种受保护的 SwingWorker 方法。我已经更新了我的答案。您的侦听器现在应该接收这些进度事件。
  • +1 for setProgress(),可以在后台线程调用;我已经详细说明了publishhere
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 2012-12-15
相关资源
最近更新 更多