2016-08-23 47 views
0

私のアプリケーションでは、テキストをTextAreaに2分ごとに追加しています。これまでにTextAreaに新しい行を追加すると、自動スクロールが自動的に停止します。しかし、私はスクロールボタンを押し続けているスクロールのままにしたい。どのようにJavaFXでそれを行う。JavaFX TextAreaの自動スクロールを制御する方法は?

logTextArea.appendText("Here i am appending text to text area"+"\n"); 
logTextArea.setScrollTop(Double.MIN_VALUE); 

私はこれを試みたが、ダウンに行く自動的にスクロールが、私は自動的にダウン行きたくない私のスクロール選択された位置を維持する必要があります。

どうすればいいですか?

答えて

0

何もしないTextAreaにchangeListenerを追加するだけで、その内部のテキストが変更されるたびにTextAreaの上部にスクロールすることができます。

logTextArea.textProperty().addListener(new ChangeListener<Object>() { 
    @Override 
    public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) { 
     logTextArea.setScrollTop(Double.MIN_VALUE); //this will scroll to the top 
    } 
}); 

今、あなたはlogTextArea.appendText("Here i am appending text to text area"+"\n");のTextAreaに、それが一番上に滞在する必要があるとき。最も簡単な方法は、それがappendTextまたはsetTextによって移動します後キャレットの位置を覚えているし、それを復元することですJavaFX TextArea and autoscroll

+0

ここで私はこれを追加する必要がありますか?クラスコントローラクラスまたはアプリケーションの拡張クラス@ user2882590を意味します。 – epn

+0

TextAreaを宣言したコントローラクラスがあります。正しく覚えていれば、 'public void initialize()'関数では、fxmlファイルから来た場合、このリスナーを 'logTextArea'に追加することができます。そうでない場合は、作成した後でこのリスナーを追加することができます。それで後: 'TextArea logTextArea = new TextArea();' – sundri23

+0

しかし、私の質問は、テキスト領域にテキストを追加した後です。私はテキストを追加すると、その間に私のボタンをスクロールしています。私はそのようにしたいだけ中間に戻って滞在する必要があります。どのようにできるのか ? – epn

1

アイデアはから取られています。ここで

は、あなたがそれを行うことができます方法は次のとおりです。

int caretPosition = area.caretPositionProperty().get(); 
area.appendText("Here i am appending text to text area"+"\n"); 
area.positionCaret(caretPosition); 
+0

これは、appendTextへのループで使用されている場合に機能します。 –

0

あなたがテキストを追加するために、独自の関数を書くことができます。この方法ではappendTextではなくを使用することができます。は自動的にコンテンツの末尾までスクロールします(setTextは先頭までスクロールしますが、これはscrollTopPropertyを元の値に戻すことで抑えることができます)。

public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      BorderPane root = new BorderPane(); 
      Scene scene = new Scene(root,400,400); 

      TextArea ta = new TextArea(); 
      root.setCenter(ta); 
      Button button = new Button("Append"); 
      button.setOnAction(e -> { 
       appendTextToTextArea(ta, "BlaBlaBla\n"); 
      }); 
      root.setBottom(button); 

      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 


    /** 
    * Appends text to the end of the specified TextArea without moving the scrollbar. 
    * @param ta TextArea to be used for operation. 
    * @param text Text to append. 
    */ 
    public static void appendTextToTextArea(TextArea ta, String text) { 
     double scrollTop = ta.getScrollTop(); 
     ta.setText(ta.getText() + text); 
     ta.setScrollTop(scrollTop); 
    } 
} 

注:

代わりに、あなたはまた、あなたがスクロールバーを移動するかどうかを指定できるようにしTextAreaや過負荷appendTextを拡張することができます。

public class AppendableTextArea extends TextArea { 

    public void appendText(String text, Boolean moveScrollBar) { 
     if (moveScrollBar) 
      this.appendText(text); 
     else { 
      double scrollTop = getScrollTop(); 
      setText(getText() + text); 
      setScrollTop(scrollTop); 
     } 
    } 
} 

および用法:

AppendableTextArea ta = new AppendableTextArea(); 
ta.appendText("BlaBlaBla\n", false); 
関連する問題