我正在开发
java上的聊天应用程序,我想使用facebook使用的功能来检索用户向上滚动时的聊天记录.我尝试了“在滚动”动作,每当滚动到达滚动条的顶部或下方时触发事件.现在我只想在滚动条到达facbook聊天框中的顶部时才开始动作事件. 最佳答案 这是你想要的例子.我希望这能帮到您.
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();
}
}