2017-06-28 46 views
5

我創建了一個簡單的GUI,其中有一個TextAreaTextArea本身將由Array填充,其中包含.txt文件中已掃描的字符串。在JavaFX中使用大的txt文件(TextArea的替代方案?)

這適用於較小尺寸的文件。但是,當使用大文件(每txt.-file大約5MB)時,TextArea(以及僅TextArea)會感覺遲緩且緩慢(不如我想要的那樣快速響應)。是否有替代TextArea(不必在JavaFX)?

我在找東西很簡單,基本上可以讓我得到&設置文字。 Slidercontrol,如JavaFXTextArea,將非常方便。

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

編輯:我的代碼一個很簡單的例子:

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

最大容量數據控制具有虛擬模式',以提供數據(所需的行)的一部分。但它並不簡單,就像get&set –

+0

您需要創建一個[Minimal,Complete和Verifiable示例](https://stackoverflow.com/help/mcve)併發布代碼。 – Sedrick

+0

@SedrickJefferson下面是一個示例,如指南中所述(編輯開始帖子) – Matt

回答

5

根據@ Matt的answer和@ SedrickJefferson的suggestion,這裏有一個完整的例子。在Java中

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

感謝@SedrickJefferson我用ListView取代TextArea。它現在運行非常流暢。除此之外,由於性能的原因,我將用替換Scanner