JavaFX 2: Making a ScrollPane automatically scroll to the edge after adding content

前端 未结 2 1606
慢半拍i
慢半拍i 2021-02-10 18:22

Using JavaFX 2, I have a basic example of a ScrollPane that contains an HBox of Labels. I want to be able to add a Label to

相关标签:
2条回答
  • 2021-02-10 19:22

    you can bind to Hbox widthproperty chnages .

    Sample Code :

       //in start method add this code
        DoubleProperty wProperty = new SimpleDoubleProperty();
        wProperty.bind(chatBox.widthProperty()); // bind to Hbox width chnages
        wProperty.addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue ov, Object t, Object t1) {
               //when ever Hbox width chnages set ScrollPane Hvalue
             chatBoxScrollPane.setHvalue(chatBoxScrollPane.getHmax()); 
            }
        }) ;
    

    and

     // remove below line from your addChatItem() method   
     chatBoxScrollPane.setHvalue(chatBoxScrollPane.getHmax());
    

    Result :yes added numbering for fun ;)

    enter image description here

    0 讨论(0)
  • 2021-02-10 19:22

    In my case I needed to enclose a HBox called labels inside a ScrollPane called msgs and here is the way I handled autoscroll.

    NOTE: The changeListener added to labels HBox is implemented via Lambda syntax(available in JDK8)

    labels.heightProperty().addListener((observable, oldVal, newVal) ->{
            msgs.setVvalue(((Double) newVal).doubleValue());
        });
    
    0 讨论(0)
提交回复
热议问题