Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing large text files with JavaFX using TextArea

Is there a way to make editing a relatively large text file (ex. 10-25 MB) in a TextArea reasonably fast? Or maybe there are features that can be disabled to make it faster? Is there an alternative component? (I know about RichTextFX, but I imagine it'd be slower since it does more, and I only need a basic editor.)

I'd rather not break the source text up into smaller parts and only load a portion of the text, since that would break text selection+copy (ie. "select all" would only be selecting the loaded text rather than the entire file's text).

like image 539
Manius Avatar asked Sep 03 '25 10:09

Manius


1 Answers

One approach would be to leverage the flyweight rendering afforded by ListView to create a line editor. Starting from this example, the LineEditor below enables multiple selection by setting SelectionMode.MULTIPLE. It also enables editing, as shown here by @tarrsalah. Naturally, you'll want to add additional controls to meet your specific use case.

image

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

/** @see https://stackoverflow.com/a/44823611/230513 */
public class LineEditor 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);
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        listView.setCellFactory(TextFieldListCell.forListView());
        listView.setOnEditCommit(new EventHandler<ListView.EditEvent<String>>() {
            @Override
            public void handle(ListView.EditEvent<String> t) {
                listView.getItems().set(t.getIndex(), t.getNewValue());
            }
        });
        listView.setEditable(true);
        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 123
trashgod Avatar answered Sep 04 '25 23:09

trashgod