gpt4 book ai didi

java - 使用 TextArea 使用 JavaFX 编辑大文本文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:18 29 4
gpt4 key购买 nike

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

我宁愿不将源文本分成更小的部分,只加载一部分文本,因为这会破坏文本选择+复制(即“全选”只会选择加载的文本,而不是选择整个文件的文本)。

最佳答案

一种方法是利用 flyweight ListView 提供的效果图创建行编辑器。从此example开始,下面的 LineEditor 通过设置 SelectionMode.MULTIPLE 启用多选。它还可以进行编辑,如图所示 here通过 @tarrsalah .自然地,您会希望添加额外的控件来满足您的特定用例。

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

关于java - 使用 TextArea 使用 JavaFX 编辑大文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46386448/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com