2016-08-05 137 views
7

這是我的一段代碼。JavaFX:textArea.setScrollTop(Double.MAX_VALUE)並不總是有效

textArea.setText(someNewText) 
textArea.positionCaret(textArea.getText().length()); 
textArea.setEditable(true); 
textArea.setScrollTop(Double.MAX_VALUE); 

我用textArea.setScrollTop(Double.MAX_VALUE)滾動文本區域的底部(解決方案,我在互聯網上找到)。它有效,但並非總是如此。我注意到,只有在調用此代碼之前垂直滾動條不可見並且代碼執行後纔可見,它才能工作。在調用此代碼之前,垂直滾動條可見時,滾動到底部始終工作。如何解決它?也許我應該讓垂直滾動條始終可見?如果是,那麼如何 - 我沒有找到解決方案。

編輯: 這是示例代碼:

public class JavaFxApp1 extends Application{ 

    private TextArea textArea; 

    @Override 
    public void start(Stage stage) throws Exception { 
     Button button=new Button("Press here"); 
     textArea=new TextArea(); 
     VBox vbox = new VBox(button,textArea); 
     button.setOnAction((event)->{ 
      textArea.appendText("###This is a very long string:some text some text some text some text some" 
        + " text some text some text some text some text some text" 
        + " text some text some text some text some text some text" 
        + " text some text some text some text some text some text .\n"); 
      textArea.selectEnd(); 
      textArea.deselect(); 
      textArea.setScrollTop(Double.MAX_VALUE); 
     }); 
     textArea.setEditable(true); 
     textArea.setWrapText(true); 
     textArea.setStyle("-fx-font-size:14px;-fx-focus-color: transparent;-fx-font-family: monospace;"); 
     Scene scene=new Scene(vbox); 
     stage.setTitle("SomeTitle"); 
     stage.setScene(scene); 
     stage.setMinHeight(400); 
     stage.setMinWidth(800); 
     stage.show(); 
    } 
} 

這是當我按下按鈕4次結果: enter image description here 正如你看到它沒有滾動至底部。在我再次按下按鈕(第五次)後,我得到以下結果: enter image description here 現在,如您所見,它被滾動到底部。 我嘗試添加:

ScrollPane scrollPane = (ScrollPane) textArea.lookup(".scroll-pane"); 
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS); 

,使滾動條始終可見 - 它是可見的,但經過4次反正不滾動至底部。

如何解決?

+0

這是一個錯誤,所以我打開了一個錯誤報告。請參閱https://bugs.openjdk.java。net/browse/JDK-8189732 –

回答

1

如果要滾動到頂部,如果你想滾動至底部,您的代碼將是這個樣子

ta.selectEnd(); 
ta.deselect(); 

現在讓TextArea您的代碼將是這個樣子

ta.selectHome(); 
ta.deselect(); 

擔心其可見區域

編輯

也許我沒有得到你的需求,導致老老實實這是我第一次看到這樣的方法給基於你的假設

也許我應該做垂直滾動條總是可見的答案嗎?如果是的話,那麼如何

以及多數民衆贊成容易TextArea使用ScrollPane,時有可見滾動 此行TextArea.getChildrenUnmodifiable().size();將檢查出是1,所以你釣出來當你創建TextArea

TextArea.getChildrenUnmodifiable().addListener(new ListChangeListener<Node>() { 
     @Override 
     public void onChanged(javafx.collections.ListChangeListener. 
      Change<? extends Node> c) { 
      while(c.next()){ 
       if(c.wasAdded()){ 
        for(Node n : TextArea.getChildrenUnmodifiable()){ 
         if(n.getClass().isAssignableFrom(ScrollPane.class)){ 
          //just trying to be cool here ^^ 
          ScrollPane sp = (ScrollPane) n; 
          sp.setVbarPolicy(ScrollBarPolicy.ALWAYS); 
         } 
        } 
       } 
      } 
     } 
    }); 

其實你可以保持到ScrollPane參考,並隨時要滾動至底部ScrollPane.setVvalue(1.0);頂端ScrollPane.setVvalue(0.0);等等等等

你可以用ScrollPane.getVvalue()得到價值。

+0

謝謝你的想法,但它沒有幫助。看我的編輯。 –

+0

@ JimJim2000刪除此行bro'textArea.setScrollTop(Double.MAX_VALUE);' – Elltz

+0

我試過這兩行,沒有它 - 結果相同。 –