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

只谈情不闲聊 提交于 2019-12-03 08:47:23

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 ;)

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());
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!