I have a JavaFX program where I want to import a file and read everything from this file (should be .txt) and write it to String, so I can work on it. Since files can sometimes be large and leave my No Answer program and the like, I would like to add a ProgressBar to show the current progress and stop the program from crashing.
At first, I just added a ProgressBar to the dialog that opens and updates the binding. But the GUI was not updating, so I found out that I need to make a new thread that works in the background, so my program does not stop responding. Therefore, I collected everything in the Task and started the task with Thread, but now Dialog with the ProgressBar does not even appear, so I debugged it and found out that the program did not even complete the task, it was just like going through it, doing nothing.
Here is my current code, I hope someone can help me with my problem or explain tasks / topics to me:
Myhandler:
package application.handler;
import application.data.KTChat;
import application.gui.MyRootPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class MyHandler implements EventHandler<ActionEvent> {
private MyRootPane mp;
private FileChooser fc;
private Stage primaryStage;
private KTChat kt;
public MyHandler(MyRootPane mp, Stage primaryStage) {
this.mp = mp;
fc = new FileChooser();
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text files (*.txt)", "*.txt"));
this.primaryStage = primaryStage;
kt = new KTChat(mp);
}
@Override
public void handle(ActionEvent event) {
String mdata = ((MenuItem)(event.getSource())).getId();
if(mdata.equalsIgnoreCase("import")) {
kr.readFile(fc.showOpenDialog(primaryStage));
}
}
public KTChat getKT() {
return kt;
}
}
MyRootPane (my GUI):
package application.gui;
import java.io.File;
import application.handler.MyHandler;
import javafx.beans.property.DoubleProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MyRootPane extends BorderPane {
private TextArea eingabe = new TextArea();
private TextArea ausgabe = new TextArea();
private MenuBar mb = new MenuBar();
private Menu m2 = new Menu("Settings");
private MenuItem mi4 = new MenuItem("Import chat");
private Stage primaryStage;
private MyHandler mh;
private FortschrittDialog fd = new FortschrittDialog();
public MyRootPane(Stage primaryStage) {
this.primaryStage = primaryStage;
mh = new MyHandler(this, primaryStage);
initSettings();
setTop(mb);
setCenter(eingabe);
setBottom(ausgabe);
}
public void initSettings() {
ausgabe.setEditable(false);
eingabe.setPrefHeight(500);
ausgabe.setPrefHeight(500);
mi4.setId("import");
mi4.setOnAction(mh);
m2.getItems().add(mi4);
mb.getMenus().add(m2);
}
public void eingabeSetText(String eingabe) {
this.eingabe.setText(eingabe);
}
public String eingabeGetText() {
return eingabe.getText();
}
public void startFortschrittDialog() {
fd.show();
}
public void endFortschrittDialog() {
fd.close();
}
public void isFortschrittDialogCompleted() {
if(fd.isCompleted()) endFortschrittDialog();
}
public DoubleProperty progressP() {
return fd.getPBProgressProperty();
}
}
Then my KTC where the line was supposed to be built:
package application.data;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import application.gui.MyRootPane;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableDoubleValue;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
public class KTChat {
private String chat;
private MyRootPane mp;
public KTChat(MyRootPane mp) {
this.mp = mp;
}
public void setChat(String eingabeGetText) {
this.chat = eingabeGetText;
getChat();
}
public void readFile(File chat) {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
if(chat.getName().contains("KakaoTalk_")) {
mp.startFortschrittDialog();
mp.setFortschritt(-1.0f);
String s = "";
String gesamt = "";
double laenge = 0;
try(BufferedReader brCount = new BufferedReader(new FileReader(chat))) {
while((s=brCount.readLine())!=null) {
laenge++;
}
} catch (IOException e) {
System.out.println("Fehler beim zählen");
}
double momentanErreicht = 0;
try(BufferedReader br = new BufferedReader(new FileReader(chat))) {
while((s=br.readLine())!=null) {
momentanErreicht++;
updateProgress(momentanErreicht, laenge);
s = s.replace("ß", "ß");
s = s.replace("ö", "ö");
s = s.replace("ü", "ü");
s = s.replace("ä", "ä");
s = s.replace("Ä", "Ä");
s = s.replace("Ü", "Ü");
s = s.replace("Ö", "Ö");
gesamt += s+"\n";
}
} catch (FileNotFoundException e1) {
System.out.println("File not found");
} catch (IOException e2) {
System.out.println("IOException");
}
mp.isFortschrittDialogCompleted();
mp.eingabeSetText(gesamt);
setChat(mp.eingabeGetText());
getChat();
} else mp.mhNichtPassendesFile();
return null;
}
};
mp.progressP().bind(task.progressProperty());
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}
Dialog where is my ProgressBar:
package application.gui;
import javafx.beans.property.DoubleProperty;
import javafx.scene.control.Dialog;
import javafx.scene.control.ProgressBar;
@SuppressWarnings("rawtypes")
public class FortschrittDialog extends Dialog {
private ProgressBar pb = new ProgressBar();
public FortschrittDialog() {
pb.setPrefWidth(500);
pb.setProgress(-1f);
getDialogPane().setContent(pb);
}
public DoubleProperty getPBProgressProperty() {
return pb.progressProperty();
}
public boolean isCompleted() {
if(pb.getProgress()==1.0) return true;
else return false;
}
}
And last but not least: Main class:
package application;
import application.gui.MyRootPane;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
MyRootPane root = new MyRootPane(primaryStage);
Scene scene = new Scene(root,1280,720);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("KT-Chat-Statistics V1.1");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
, , , . , .