Editing large text files with JavaFX using TextArea

送分小仙女□ 提交于 2021-02-07 19:47:34

问题


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).


回答1:


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.

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


来源:https://stackoverflow.com/questions/46386448/editing-large-text-files-with-javafx-using-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!