2017-09-24 2 views
3

TextAreaの比較的大きなテキストファイル(例:10-25 MB)を合理的に高速に編集する方法はありますか?または、それを速くするために無効にできる機能がありますか?代わりのコンポーネントはありますか? (私はRichTextFXについて知っていますが、それ以上のことは遅いと思っています。基本的なエディタが必要です)TextAreaを使用してJavaFXで大きなテキストファイルを編集する

私は、ソーステキストを小さな部分に分割し、つまり、テキスト選択+コピーを中断することになります(つまり、「すべて選択」はファイルのテキスト全体ではなく読み込まれたテキストのみを選択することになります)。

答えて

3

ListViewによって提供されるflyweightレンダリングを利用してラインエディタを作成する方法があります。このexampleから、LineEditorは、SelectionMode.MULTIPLEを設定することによって複数の選択を可能にします。また、hereによって@tarrsalahのように編集することができます。もちろん、特定のユースケースに合わせて追加のコントロールを追加する必要があります。

image

import java.io.*; 
import javafx.application.*; 
import javafx.collections.*; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.control.cell.*; 
import javafx.scene.layout.*; 
import javafx.stage.Stage; 

/** @see https://stackoverflow.com/a/44823611/230513 */ 
public class LineEditor 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); 
     listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); 
     listView.setCellFactory(TextFieldListCell.forListView()); 
     listView.setOnEditCommit(new EventHandler<ListView.EditEvent<String>>() { 
      @Override 
      public void handle(ListView.EditEvent<String> t) { 
       listView.getItems().set(t.getIndex(), t.getNewValue()); 
      } 
     }); 
     listView.setEditable(true); 
     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(); 
    } 
} 
+0

私は「最高」の回答が全体の新しいカスタムTextAreaコンポーネントが必要であることになるだろう...しかし、これは試してみる興味深い妥協点であると思われます。 – Manius

+1

@Manius: 'ListView'は、IndexedCellを認める他のビューと同様に、サイズの要件を満たす必要があります。 'TableView'。大きなファイルやリモートファイルシステムには、 'Task 'が必要な場合があります。同様の質問が[こちら](https://stackoverflow.com/q/27414689/230513)で調べられます。 – trashgod

+1

素敵な発見、私は私の前の検索でそのリンクに気付かなかった。 – Manius

関連する問題