我创建了一个简单的 GUI,其中有一个 TextArea
。 TextArea
本身将由一个 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 , 这是一个完整的例子。
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/