用不同的参数调用不同地方的摇摆工人

Fir*_*iew 2 java swing swingworker

我正在使用swing worker来验证文件,我希望将startLine和endLine发送到validate类,因为我不想每次都验证整个文件.第一次当现有文件是openend时,我想将startLine发送为0,将endLine作为endLine = editorTextArea.getLineCount() - 1;发送.之后我应该能够每秒钟将startLine和endLine发送给我.我如何实现这一目标?

验证课程:

  class Validate implements Runnable {

     private JTextArea editorTextArea;
     private JTextArea errorTextArea;
     private int startLine;
     private int endLine;

     public Validate(JTextArea editor, JTextArea error, startLine, endLine) {
         this.editorTextArea = editor;
         this.errorTextArea  = error;
         this.startLine = startLine;
         this.endLine = endLine;        
    }

@Override
public void run() {
    SwingWorker worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            //CODE TO VALIDATE
            return null;
        }

        @Override
        protected void done() {                
            //CODE TO DISPLAY THE RESULT
        }
    };
    worker.execute();
    }
 }
Run Code Online (Sandbox Code Playgroud)

主要课程:

  //Calls the validate
   public void taskExecutor() {               
          ScheduledExecutorService scheduler =
                 Executors.newSingleThreadScheduledExecutor();
           final ScheduledFuture<?> timeHandle =
                 scheduler.scheduleAtFixedRate(new Validate(editorTextArea,   errorTextArea), 0, 1, SECONDS);                 
}


    private void openFileActionPerformed(java.awt.event.ActionEvent evt)      {                                      

  fileChooser.setCurrentDirectory(new File(".txt"));
int result = fileChooser.showOpenDialog(new JPanel());
int totLines = 0;
String[] content = null;

if (result == JFileChooser.APPROVE_OPTION) {
    try {
        filename = String.valueOf(fileChooser.getSelectedFile());
        setTitle(filename);

        FileReader fr = new FileReader(filename);
        editorTextArea.read(fr, null);
        fr.close();
        startLine = 0;
        endLine = editorTextArea.getLineCount() - 1;
        //here i want to call the validate class once. that is askExecutor();                
    } catch (IOException ex) {
        System.out.println(ex);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

tra*_*god 5

A SwingWorkerExecutorService内部使用,但工作人员"只能执行一次".我不明白为什么你要Runnable按照固定费率安排工人.相反,execute()任务一次,并让它在EDT publish()process()编辑中期结果.对于行号,SwingWorker<Boolean, Integer>可能是合适的.Integer将代表处理的最后一个行号,而Boolean代表返回的最终验证结果doInBackground().

如果用户异步选择多个文件进行验证,请考虑将每个正在执行的工作程序添加到合适的文件TableModel并在相应的位置显示结果JTable.@mKorbel已经显示出几个例子为特色JProgressBar.

附录:如果您正在验证添加内容,则每次JTextArea都需要execute()新工作人员,将新的行号作为参数传递给工作人员的构造函数.这个传递单个的例子int count可能暗示了这种方法.触发器可以是a java.util.Timer,a javax.swing.Timer甚至是ScheduledExecutorService你最初提出的.