[英]Calling swing worker at different places with different parameters
I am using swing worker to validate files, I want the startLine and endLine to be sent to the validate class as i do not want to validate the whole file every time. 我正在使用swing worker来验证文件,我希望将startLine和endLine发送到validate类,因为我不想每次都验证整个文件。 For the first time when an existing file is openend, i want to send the startLine as 0 and endLine as endLine = editorTextArea.getLineCount() - 1;.
第一次,当现有文件打开时,我想将startLine发送为0,将endLine发送为endLine = editorTextArea.getLineCount()-1;。 After that i should be able to send the startLine and endLine to my convenience every second.
之后,我应该能够每秒发送一次startLine和endLine给我方便。 How do i achieve this?
我该如何实现?
Validate class: 验证类别:
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();
}
}
Main class: 主班:
//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);
}
}
}
A SwingWorker
uses an ExecutorService
internally, but the worker "is only designed to be executed once." SwingWorker
在内部使用ExecutorService
,但是worker“仅设计为执行一次”。 I don't see why you're wrapping the worker in a Runnable
that's scheduled at a fixed rate. 我不明白为什么要在固定速率的
Runnable
中包装工人。 Instead, execute()
the task once and let it publish()
interim results that can be process()
ed on the EDT. 取而代之的是,一次
execute()
任务,然后让其publish()
临时结果,该结果可以在EDT上进行process()
。 For line numbers, SwingWorker<Boolean, Integer>
may be appropriate. 对于行号,
SwingWorker<Boolean, Integer>
可能是合适的。 Integer
would represent the last line number processed, while Boolean
would represent the final validation result returned by doInBackground()
. Integer
将表示最后处理的行号,而Boolean
将表示由doInBackground()
返回的最终验证结果。
If the user is aynchronously selecting multiple files for validation, consider adding each executing worker to a suitable TableModel
and displaying the results in the corresponding JTable
. 如果用户正在异步选择多个文件进行验证,请考虑将每个执行工作程序添加到合适的
TableModel
,并将结果显示在相应的JTable
。 @mKorbel has shown several examples featuring JProgressBar
. @mKorbel显示了几个具有
JProgressBar
示例 。
Addendum: If you're validating additions to the JTextArea
, you'll want to execute()
a new worker each time, passing the new range of line numbers as parameters to the worker's constructor. 附录:如果您要验证对
JTextArea
添加 ,则需要每次execute()
一次新工作器,并将新的行号范围作为参数传递给该工作器的构造函数。 This example , which passes a single int count
, may suggest the approach. 此示例通过单个
int count
,可能表明该方法。 The trigger can be a java.util.Timer
, a javax.swing.Timer
or even the ScheduledExecutorService
you originally proposed. 触发器可以是
java.util.Timer
, javax.swing.Timer
甚至是您最初建议的ScheduledExecutorService
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.