Я использую 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();
}
}
Основной класс:
//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);
}
}
}