在JavaFX中使用大型txt文件(TextArea替代?) [英] Using large txt files in JavaFX(TextArea alternatives?)
问题描述
我创建了一个简单的GUI,我有 TextArea
。 TextArea
本身将由数组
填充,其中包含中扫描的字符串。 txt
file。
这适用于较小尺寸的文件。当使用大文件(每个文本文件大约5MB)时, TextArea
(只有 TextArea
)感觉迟滞和缓慢(不像我想的那样响应)。是否有替代 TextArea
(不必在 JavaFX
)?
我正在寻找一些非常简单的东西,这基本上可以让我得到&设置文本。 Slidercontrol
,如 JavaFX
TextArea
,会非常方便。
谢谢你,祝你有美好的一天!
编辑:我代码的一个非常基本的例子:
公共类Main扩展应用程序{
public void start(阶段阶段){
Pane pane = new Pane();
TextField filePath = new TextField(Filepath进入这里......);
TextArea file = new TextArea(导入的文件字符串在这里......);
file.relocate(0,60);
按钮btnImport = new按钮(导入文件);
btnImport.relocate(0,30);
ArrayList< String> data = new ArrayList<>();
btnImport.setOnAction(e - > {
文件fileToImport =新文件(filePath.getText());
尝试{
扫描仪扫描仪=新扫描仪( fileToImport);
while(scanner.hasNextLine()){
data.add(scanner.nextLine());
}
file.setText(data.toString()) ;
} catch(FileNotFoundException e1){
e1.printStackTrace();
}
});
pane.getChildren()。addAll(filePath,file,btnImport);
场景场景=新场景(窗格);
stage.setScene(场景);
stage.show();
}
public static void main(String [] args){
launch();
}
}
基于在@Matt的
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;
公共类Main扩展Application {
public static void main(String [] args){
launch(args);
}
@Override
public void start(阶段阶段){
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);
场景场景=新场景(窗格);
stage.setScene(场景);
stage.show();
}
}
I created a simple GUI where I have a TextArea
. The TextArea
itself will be populated by an Array
, which contains scanned Strings out of a .txt
file.
This works great for smaller size files. When using large files(~5MB per txt.-file) however, the TextArea
(and only the TextArea
) feels laggy and slow(not as responsive as I would like). Is there an alternative to the TextArea
(doesnt have to be in JavaFX
)?
I'm looking for something very simple, which basically allows me to get & set text. Slidercontrol
, as in JavaFX
TextArea
, would be very handy.
Thank you and have a great day!
Edit: a very basic example of my code:
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();
}
}
Based on @Matt's answer and @SedrickJefferson's suggestion, here's a complete example.
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();
}
}
这篇关于在JavaFX中使用大型txt文件(TextArea替代?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!