0

I have a little problem.

Here is my code

package email;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;

public class Controller {

@FXML
public static Label daten;


@FXML
public static Button Datei;

@FXML
public TextField Trennzeichen;

static int i = 0;


@FXML
public void Datei(ActionEvent event) throws IOException, InterruptedException {

FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(null);

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);

String[] buffer = new String[9];
String[][] data = new String[2000][9];

try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String zeile = reader.readLine();
while (zeile != null) {
if ((zeile.substring(0, 1).equals("I")) || (zeile.substring(0, 1).equals("-"))) {
System.out.println("ha");
} else {

buffer = zeile.split(Trennzeichen.getText());

for (int t = 0; t < buffer.length; t++) {
data[i][t] = buffer[t];
}
++i;
daten.setText("" + i);

}
zeile = reader.readLine();
}

} catch (IOException e) {
e.printStackTrace();
}

}
}

I have a Label on my GUI, thats have to show me how many lines are in my txt file.

This works fine, but it is not live, the gui update after the work, before is the gui freezy..

How can I fix this in a task?

short again:

I have 1 label filechooser thats load a txt file the label have to show how many lines are reading after every line

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Patrik
  • 23
  • 6

1 Answers1

2

See the question and my answer here: Display progress bar while compressing video file

JavaFX is a single thread GUI toolkit and if you do a long running task on the GUI Thread, it will freeze. So you should move the file reading to a background task as described in the linked question.


Edit: Sample skeleton solution

public void readFile() {
    Label counter = new Label();
    ReadFileTask task = new ReadFileTask();
    counter.textProperty().bind(task.progressProperty().asString());
    new Thread(task).start();
}

public class ReadFileTask extends Task<Void> {

    @Override
    protected Void call() throws Exception {

        File file = new File(pathname); //TODO
        long lines = Files.lines(file.toPath()).count();
        long line = 0;
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String zeile = null;
        while ((zeile = reader.readLine()) != null) {
            //TODO do the work

            updateProgress(line++, lines);
        }

        return null;
    }

}
Community
  • 1
  • 1
eckig
  • 10,964
  • 4
  • 38
  • 52
  • If the linked answer answers the question, then you should vote to close it as a duplicate or leave it as a comment – MadProgrammer Dec 31 '14 at 20:47
  • @MadProgrammer based on the question I was prepared to write (once again) the complete solution as an answer, because it seems he did not grasp much of the concepts involved. But generally speaking, you are right ;) – eckig Dec 31 '14 at 20:49
  • @MadProgrammer maybe u can help me whit this line? long lines = Files.lines(file.toPath()).count(); they say me cannot find symbol, (.lines) – Patrik Dec 31 '14 at 21:43
  • @Patrik According to the [JavaDocs](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html), there is no such method as `Files.lines`, did you mean `Files.readAllLines`? – MadProgrammer Dec 31 '14 at 22:01
  • It is a java 8 method of java nio to stream all lines of a file. – eckig Dec 31 '14 at 22:31
  • A very Big merci and happy new year :) i realy trying 4-5days for this.. now its working, only 1 problem left, when i debug then i see better the problem. start he say me counter = -1.0 then 0 then 0.125 then 0.25 ... finish at 0.875 but they are 8 lines in the txt file :D he try to get me 1.0 for 100%, like a progress bar i think? ;) i try to get the numbers of lines, i try self now, maybe when i can fix this someone can help? when not i try again :D – Patrik Jan 01 '15 at 01:32
  • try `updateMessage()` in the Task and use `counter.textProperty().bind(task.messageProperty());` in your GUI. – eckig Jan 01 '15 at 09:35
  • Thanks, in the Work, he read a line and then comes: if (buffer[0].endsWith("abc")...{ do ... ... } in the do work he has another counter for counting abc.. for example if (budder[0].endsWith("abc"){do ... abc++; ... } and the abc is another label in the gui. can i use this in the same task? its works but i can not usw updateMessage("" + abc ); gives a way or a new task? – Patrik Jan 01 '15 at 12:10
  • I have no idea what you are talking about. But if that is a follow up question please post a new question instead of the comments. – eckig Jan 01 '15 at 12:18