java - 在 JavaFX 中使用大型 txt 文件(TextArea 替代品?)

标签 java javafx

我创建了一个简单的 GUI,其中有一个 TextAreaTextArea 本身将由一个 Array 填充,其中包含来自 .txt 文件的扫描字符串。

这适用于较小的文件。但是,当使用大文件(每个 txt.-file 约 5MB)时,TextArea(并且只有 TextArea)感觉迟钝和缓慢(不像我想要的那样响应) .是否有 TextArea 的替代方案(不必在 JavaFX 中)?

我正在寻找非常简单的东西,它基本上允许我获取和设置文本。 Slidercontrol,如在 JavaFX TextArea 中,会非常方便。

谢谢你,祝你有美好的一天!

编辑:我的代码的一个非常基本的例子:

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

最佳答案

基于@Matt 的 answer和@SedrickJefferson 的 suggestion , 这是一个完整的例子。

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

关于java - 在 JavaFX 中使用大型 txt 文件(TextArea 替代品?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44796527/

相关文章:

java - 创建新模块时不允许我创建类文件

Java:搜索未排序的整数数组并返回有多少个数字更大

java - 保存多个多对一关系时的 Hibernate 优化

JavaFX:没有 JavaFX 样式属性的可编辑 TableView

java - 在 javafx 2 中将 Rectangle 传递给 setContent 时我做错了什么?

java - 将 java 项目导出为 JAR 并尝试在 ECLIPSE 的动态 Web 项目中使用它后,找不到资源文件

java - Android:如何将日期舍入到下一小时的 00

java - 不工作 ccs 样式 javafx -fx-strikethrough

java - 为什么JavaFX不支持.wav格式?

java - Java Swing 应用程序中的简单 JavaFX WebView 不显示内容