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. 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;. After that i should be able to send the startLine and endLine to my convenience every second. 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
SwingWorkeruses anExecutorServiceinternally, but the worker “is only designed to be executed once.” I don’t see why you’re wrapping the worker in aRunnablethat’s scheduled at a fixed rate. Instead,execute()the task once and let itpublish()interim results that can beprocess()ed on the EDT. For line numbers,SwingWorker<Boolean, Integer>may be appropriate.Integerwould represent the last line number processed, whileBooleanwould represent the final validation result returned bydoInBackground().If the user is aynchronously selecting multiple files for validation, consider adding each executing worker to a suitable
TableModeland displaying the results in the correspondingJTable. @mKorbel has shown several examples featuringJProgressBar.Addendum: If you’re validating additions to the
JTextArea, you’ll want toexecute()a new worker each time, passing the new range of line numbers as parameters to the worker’s constructor. This example, which passes a singleint count, may suggest the approach. The trigger can be ajava.util.Timer, ajavax.swing.Timeror even theScheduledExecutorServiceyou originally proposed.