Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using large txt files in JavaFX(TextArea alternatives?)

Tags:

java

javafx

I created a simple GUI where I have a TextArea. The TextArea itself will be populated by an Array, which contains scanned Strings out of a .txt file.

This works great for smaller size files. When using large files(~5MB per txt.-file) however, the TextArea(and only the TextArea) feels laggy and slow(not as responsive as I would like). Is there an alternative to the TextArea(doesnt have to be in JavaFX)?

I'm looking for something very simple, which basically allows me to get & set text. Slidercontrol, as in JavaFX TextArea, would be very handy.

Thank you and have a great day!

Edit: a very basic example of my code:

public class Main extends Application {

public void start(Stage stage) {
    Pane pane = new Pane();
    TextField filePath = new TextField("Filepath goes in here...");
    TextArea file = new TextArea("Imported file strings go here...");
    file.relocate(0, 60);
    Button btnImport = new Button("Import file");
    btnImport.relocate(0, 30);
    ArrayList<String> data = new ArrayList<>();

    btnImport.setOnAction(e -> {
        File fileToImport = new File(filePath.getText());
        try {
            Scanner scanner = new Scanner(fileToImport);
            while(scanner.hasNextLine()) {
                data.add(scanner.nextLine());
            }
            file.setText(data.toString());
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    });

    pane.getChildren().addAll(filePath, file, btnImport);
    Scene scene = new Scene(pane);
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args){
    launch();
}
}
like image 302
Matt Avatar asked Jun 28 '17 07:06

Matt


2 Answers

Based on @Matt's answer and @SedrickJefferson's suggestion, here's a complete example.

image

import java.io.*;
import javafx.application.*;
import javafx.collections.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        VBox pane = new VBox();
        Button importButton = new Button("Import");
        TextField filePath = new TextField("/usr/share/dict/words");
        ObservableList<String> lines = FXCollections.observableArrayList();
        ListView<String> listView = new ListView<>(lines);
        importButton.setOnAction(a -> {
            listView.getItems().clear();
            try {
                BufferedReader in = new BufferedReader
                    (new FileReader(filePath.getText())); 
                String s;
                while ((s = in.readLine()) != null) {
                    listView.getItems().add(s);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        pane.getChildren().addAll(importButton, filePath, listView);
        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.show();
    }
}
like image 86
trashgod Avatar answered Oct 09 '22 22:10

trashgod


Thanks to @SedrickJefferson I replaced the TextArea with a ListView. It runs very smooth now. Besides that I will replace the Scanner with a BufferedReader, due to performance issues.

like image 27
Matt Avatar answered Oct 09 '22 22:10

Matt