本文介绍了JavaFX - 如何在TableView中识别ScrollBar的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我写过这个小例子应用程序:I have written this little example application:package application;import javafx.application.Application;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.control.TableColumn;import javafx.scene.control.TableColumn.CellEditEvent;import javafx.scene.control.TableView;import javafx.scene.control.cell.PropertyValueFactory;import javafx.scene.control.cell.TextFieldTableCell;import javafx.scene.input.MouseEvent;import javafx.scene.layout.StackPane;import javafx.stage.Stage;public class Main extends Application { public class Person { private StringProperty firstName = new SimpleStringProperty(); private StringProperty lastName = new SimpleStringProperty(); public Person(String firstName, String lastName) { this.firstName.set(firstName); this.lastName.set(lastName); } public String getFirstName() { return firstName.get(); } public String getLastName() { return lastName.get(); } public StringProperty firstNameProperty() { return firstName; } public StringProperty lastNameProperty() { return lastName; } } @Override public void start(Stage primaryStage) { try { StackPane root = new StackPane(); TableView<Person> tv = new TableView<>(); TableColumn<Person, String> col = new TableColumn<Person, String>("FirstName"); col.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName")); tv.getColumns().add(col); tv.setEditable(true); col = new TableColumn<Person, String>("LastName"); col.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName")); col.setCellFactory(TextFieldTableCell.forTableColumn()); col.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() { @Override public void handle(CellEditEvent<Person, String> event) { System.out.println(tv.getItems().get(1).getLastName()); } }); tv.getColumns().add(col); for (int i = 0; i < 30; i++) { tv.getItems().add(new Person("Test" + i, "Test" + i)); } root.getChildren().add(tv); Scene scene = new Scene(root, 400, 200); primaryStage.setScene(scene); primaryStage.show(); tv.addEventFilter(MouseEvent.MOUSE_RELEASED, event -> { // ... }); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); }}我想在 ScrollBar 已达到最低点。然后我想从数据库重新加载更多数据。但只有这样,当用户看到所有已经加载的数据时(=底部的滚动条)。你有什么好建议可以解决这个问题吗?I want to perform action when the ScrollBar has reached the bottom. Then I want to reload more datas from the database. But only then, when the user has seen all the already loaded datas (= scrollbar on the bottom). Do you have nice suggestions to solve this issue?我的第一个想法是抓住 MOUSE_RELEASED 事件(当时用户拖动 TableView 的栏,然后检查 ScrollBar 的位置: - getValue()获取栏的位置 - getMax()最大值(=底部)。My first idea was to catch the MOUSE_RELEASED event (when the users drags the bar) of the TableView and then to check the position of the ScrollBar:- getValue() gets the position of the bar- getMax() the maximum value (=bottom).但我找不到方法(不使用css选择器)通过此方法)从给定的 TableView 获取 ScrollBar 。所以我无法在某个 TableView 中检查它的位置。But I can't find a way (without using the css-selector via this method) to get the ScrollBar from a given TableView. So I can't check the position of it in a certain TableView.你有什么想法吗?我很兴奋。谢谢你的帮助。I am excited. Thanks for your help.推荐答案获取滚动条的唯一方法是通过查找,这有点像黑客攻击,但是只要你在在场景上渲染了之后它就会工作。你需要The only way to get the scroll bar is via a lookup, which is a bit of a hack, but it will work as long as you do it after the table has been rendered on the scene. You needScrollBar verticalBar = (ScrollBar) table.lookup(".scroll-bar:vertical");请注意,不需要弄乱用户事件:您只需观察滚动条的 value 属性直接:Note that there's no need to mess with user events: you can just observe the scroll bar's value property directly:verticalBar.valueProperty().addListener((obs, oldValue, newValue) -> { if (newValue.doubleValue() >= verticalBar.getMax()) { // add more data... }}); SSCCE:import java.util.ArrayList;import java.util.List;import java.util.function.Function;import javafx.application.Application;import javafx.beans.property.IntegerProperty;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;import javafx.beans.value.ObservableValue;import javafx.concurrent.Task;import javafx.scene.Scene;import javafx.scene.control.ScrollBar;import javafx.scene.control.TableColumn;import javafx.scene.control.TableView;import javafx.scene.layout.BorderPane;import javafx.stage.Stage;public class AddMoreTableDataOnScrollToBottom extends Application { @Override public void start(Stage primaryStage) { TableView<Item> table = new TableView<>(); table.getColumns().add(column("Item", Item::nameProperty)); table.getColumns().add(column("Value", Item::valueProperty)); addMoreData(table, 20); Scene scene = new Scene(new BorderPane(table), 400, 400); primaryStage.setScene(scene); primaryStage.show(); ScrollBar verticalBar = (ScrollBar) table.lookup(".scroll-bar:vertical"); verticalBar.valueProperty().addListener((obs, oldValue, newValue) -> { if (newValue.doubleValue() >= verticalBar.getMax()) { addMoreData(table, 20); } }); } private void addMoreData(TableView<Item> table, int numItems) { Task<List<Item>> dataRetrieveTask = new Task<List<Item>>() { @Override public List<Item> call() throws Exception { // mimic connect to db: Thread.sleep(500); List<Item> items = new ArrayList<>(); int nextItem = table.getItems().size() + 1 ; for (int i = nextItem; i < nextItem + numItems; i++ ){ items.add(new Item("Item "+i, i)); } return items ; } }; dataRetrieveTask.setOnSucceeded(e -> table.getItems().addAll(dataRetrieveTask.getValue())); new Thread(dataRetrieveTask).start(); } private <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> prop) { TableColumn<S,T> col = new TableColumn<>(title); col.setCellValueFactory(cellData -> prop.apply(cellData.getValue())); return col ; } public static class Item { private final StringProperty name = new SimpleStringProperty(); private final IntegerProperty value = new SimpleIntegerProperty(); public Item(String name, int value) { setName(name); setValue(value); } public final StringProperty nameProperty() { return this.name; } public final String getName() { return this.nameProperty().get(); } public final void setName(final String name) { this.nameProperty().set(name); } public final IntegerProperty valueProperty() { return this.value; } public final int getValue() { return this.valueProperty().get(); } public final void setValue(final int value) { this.valueProperty().set(value); } } public static void main(String[] args) { launch(args); }} 这篇关于JavaFX - 如何在TableView中识别ScrollBar的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 02:56