Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法运行时使用java-JProgressBar_Java_Swing_Sync_Swingworker_Jprogressbar - Fatal编程技术网

方法运行时使用java-JProgressBar

方法运行时使用java-JProgressBar,java,swing,sync,swingworker,jprogressbar,Java,Swing,Sync,Swingworker,Jprogressbar,Find()方法是我在一些文件中搜索单词的方法的一个示例。 我从一个按钮\u Start鼠标点击事件中调用它。 这是我的密码: public void Find (String word) { List_files_directories (directory.getAbsolutePath(), files); // print list textpane.append(java.awt.Color.cyan, "Files in the choosen director

Find()
方法是我在一些文件中搜索单词的方法的一个示例。 我从一个
按钮\u Start
鼠标点击事件中调用它。
这是我的密码:

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
);
}
在本文中,我在
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()));
    }
} 
@覆盖
受保护对象doInBackground()引发异常{

对于(int j=0;j我认为您需要将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()));
    }
} 
@覆盖
受保护对象doInBackground()引发异常{
对于(int j=0;j
什么是发布()
?我不明白我必须做什么

方法
publish()
process()
SwingWorker
从后台线程可靠地与事件调度线程(EDT)通信。在这种情况下,
process()
显示长时间运行的计算的当前结果;在这个相关的
process()
还会更新图表的数据模型

附录:@mKorbel指出,您可以通过使用更具体的类型来利用泛型类型检查,例如
SwingWorker

什么是发布()
?我不明白我必须做什么

方法
publish()
process()
SwingWorker
从后台线程可靠地与事件调度线程(EDT)通信。在这种情况下,
process()
显示长时间运行的计算的当前结果;在这个相关的
process()
还会更新图表的数据模型


附录:@mKorbel指出,您可以通过使用更具体的类型来利用泛型类型检查,例如,
SwingWorker

请不要攻击,不要不理解,可能代码的描述不同,请说明其目标是什么,可能是JTextPane,可以是Jlist或JTable,或者???,可能是我错读了一些东西以获得更好的帮助r、 发布一个。请不要攻击,不要理解,可能代码与描述不同,请说明目标是什么,可能是JTextPane,可以是Jlist或JTable,或者???,可能是我错了,误读了一些东西为了更快地获得更好的帮助,发布一个。对不起,“发布”是什么?我不明白我必须在SwingWorker上发布的是一个受保护的方法…但在这种情况下,您实际上想要使用另一个受保护的SwingWorker方法setProgress。我已经更新了我的答案。您的侦听器现在应该拾取这些进度事件。
setProgress()的+1
,可以在后台线程中调用;我已经详细介绍了
publish
。对不起,“publish”是什么?我不明白我必须在SwingWorker上发布的是一个受保护的方法…但在这种情况下,您实际上想要使用另一个受保护的SwingWorker方法setProgress。我已经更新了我的答案。您的侦听器现在应该拾取这些进度事件。
setProgress()的+1
,可以在后台线程中调用;我已经详细阐述了
发布
+1但不确定declare SwingWorker是否合适,仍然缺少这个问题的原因,(留给鲸鱼)+1但不确定declare SwingWorker是否合适,仍然缺少这个问题的原因,(留给鲸鱼)