How to fire event when scrolling up,javafx

4.4k views Asked by At

I am developing chat application on java and i want to use function used by facebook to retrieve chat history when user scroll up. I tried "on Scroll" action which fire event whenever scroll reach top or down of scroll bar. Now i want to Fire action event only when scroll bar reach top like in facbook chat box.

2

There are 2 answers

0
Dhiren Hamal On BEST ANSWER

Here is the example of what you want. I hope this will help you.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ScrollUtil extends Application 
{
    private boolean scrollToBottom = false;
    private boolean scrollToTop = false;

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

    @Override
    public void start(final Stage stage) throws Exception 
    {

        final VBox root = new VBox();
        final ScrollPane scrollPane = new ScrollPane();

        final VBox vBox = new VBox();
        vBox.setAlignment(Pos.BOTTOM_CENTER);

        scrollPane.setContent(vBox);

        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

        Button button = new Button("add");
        Button button3 = new Button("scroll up");

        button3.setOnAction(new EventHandler<ActionEvent>() 
        {
            @Override
            public void handle(ActionEvent actionEvent) 
            {
                scrollToTop = true;
                 scrollPane.setVvalue(scrollPane.getVmin());
                System.out.println("vmin= "+scrollPane.getVmin() + "; vmax = " + scrollPane.getVmax() + "; vvalue" + scrollPane.getVvalue());
            }
        });

        button.setOnAction(new EventHandler<ActionEvent>() 
        {
            @Override
            public void handle(ActionEvent actionEvent) 
            {
                vBox.getChildren().add(new Label("hallo"));
                scrollToBottom = true;
                //System.out.println(scrollPane.getVmin() + "; max = " + scrollPane.getVmax() + "; " + scrollPane.getVvalue());
            }
        });

        scrollPane.setVvalue(scrollPane.getVmax());
        scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() 
        {
          @Override
          public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) 
          {
            if(scrollToBottom)
            {
              scrollPane.setVvalue(scrollPane.getVmax());
              scrollToBottom = false;
            }

            if(newValue.doubleValue() ==0)
            {
                vBox.getChildren().add(new Label("hallo"));
                System.out.println("min value ="+scrollPane.getVmin());
                //scrollPane.setVvalue(scrollPane.getVmin());
                scrollToTop = true;
                System.out.println("now get the chat history");
            }
          }
        });

        Button button2 = new Button("scroll");
        button2.setOnAction(new EventHandler<ActionEvent>() 
        {
            @Override
            public void handle(ActionEvent actionEvent) 
            {
                scrollPane.setVvalue(Double.MAX_VALUE);
            }
        });

        root.getChildren().addAll(scrollPane, button, button2, button3);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}
0
Uluk Biy On

Assuming that you have a ScrollPane, you can observe vvalueProperty of it:

scrollPane.vvalueProperty().addListener(
    (ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
    if(newValue.doubleValue() == 0){
        System.out.println( "AT TOP" );
        // load more items
    }
});

and you may also scroll to bottom with

scrollPane.setVvalue( scrollPane.getVmax() );

initially.