使用TextArea使用JavaFX编辑大型文本文件 [英] Editing large text files with JavaFX using TextArea

查看:401
本文介绍了使用TextArea使用JavaFX编辑大型文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使TextArea中的相对较大的文本文件(例如10-25 MB)以相当快的速度进行编辑?也许有一些功能可以禁用以使其更快?是否有替代组件? (我了解RichTextFX,但是我想它会变慢,因为它能做更多,而且我只需要一个基本的编辑器.)

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

推荐答案

一种方法是利用 渲染> 创建一个行编辑器.从此示例开始,下面的LineEditor通过设置SelectionMode.MULTIPLE启用多项选择.它还可以通过

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

这篇关于使用TextArea使用JavaFX编辑大型文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆