jeudi 1 janvier 2015

JavaFX ReadFile counter + another counter task?


Vote count:

0




i have a UI whit 2 labels.


First Label: All Lines Second Label: Lines ends with xxx


My Programm Read a File and in a Task he Read all Lines:



public class ReadFileTask extends Task<Void> {

@Override
protected Void call() throws Exception {

File file = new File("C:\\Users\\patrik\\Documents\\comments.txt"); //TODO

long lines = Files.lines(file.toPath()).count();
long line = 0;
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((reader.readLine()) != null) {
line++;
updateMessage("" + line);

}
return null;
}}


Now i have the First label.


For the 2nd Label i try this:



public class ReadFileTask extends Task<Void> {

@Override
protected Void call() throws Exception {

File file = new File("C:\\Users\\patrik\\Documents\\comments.txt"); //TODO

long lines = Files.lines(file.toPath()).count();
long line = 0;
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((reader.readLine()) != null) {
line++;
updateMessage("" + line);

if (reader.readLine().endsWith("xxx")) {



}
}
return null;
}}


the problem is, i don´t know how can i set the 2nd label?



if (reader.readLine().endsWith("xxx")) { }


what i have to do? the label name is xxxLabel, when i try



int xxxLines = 0;
if (reader.readLine().endsWith("xxx")) {
xxxLines++;
xxxLabel.setText("" + xxxLines);
}


is it not working :/


if i debug i go in this line: " xxxLabel.setText.... and get a exception from task class:



catch (final Throwable th) {
// Be sure to set the state after setting the cause of failure
// so that developers handling the state change events have a
// throwable to inspect when they get the FAILED state. Note
// that the other way around is not important -- when a developer
// observes the causeOfFailure is set to a non-null value, even
// though the state has not yet been updated, he can infer that
// it will be FAILED because it can be nothing other than FAILED
// in that circumstance.
task.runLater(() -> {
task._setException(th);
task.setState(State.FAILED);
});


asked 1 min ago







JavaFX ReadFile counter + another counter task?

Aucun commentaire:

Enregistrer un commentaire