Using large txt files in JavaFX(TextArea alternatives?)

Matt :

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();
}
}
trashgod :

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();
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=472408&siteId=1