滚动时如何触发事件,javafx

时间:2015-06-26 09:14:17

标签: javafx-8 scrollpane

我正在开发java上的聊天应用程序,我想使用facebook使用的功能在用户向上滚动时检索聊天记录。我试过"在Scroll"每当滚动到达滚动条顶部或下部时触发事件的动作。现在我只想在滚动条到达顶部时才开始动作事件,就像在facbook聊天框中一样。

2 个答案:

答案 0 :(得分:1)

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.

答案 1 :(得分:1)

以下是您想要的示例。我希望这会对你有所帮助。

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