我创建了一个简单的GUI,我有一个TextArea.它TextArea本身将由a填充Array,其中包含扫描的.txt文件串.
这适用于较小尺寸的文件.然而,当使用大文件(每个文本文件大约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();
}
}
Run Code Online (Sandbox Code Playgroud)
根据@Matt 的回答和@ SedrickJefferson的建议,这是一个完整的例子.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
689 次 |
| 最近记录: |