JavaFX TableView:从表中删除/禁用scollbars(改为使用ScrollPane)

时间:2017-07-06 07:11:53

标签: java javafx

我有两个表,我想连接滚动条,因此可以同时滚动它们。据我所知,TableViews自己的滚动条的绑定是不可能的(如果我错了,请纠正我),所以我决定将每个TableView包裹在ScrollPane中。

但是,表格的滚动仍设置为TableView自己的滚动条。

Beneath是一个基本的代码示例。如您所见,ScrollPane滚动条的双向绑定有效。但是,由于我上面描述的问题(主控件是TableViews自己的滚动条),表格中的数据不受此影响。

有人可以帮我解决这个问题吗?我希望使用TableView滚动条移动ScrollPane,而不是使用TableViews滚动条(因为我无法绑定它们)。

public class Main extends Application {
    private TableView<Person> table = new TableView<>();
    private TableView<Person> table2 = new TableView<>();
    private final ObservableList<Person> data =
            FXCollections.observableArrayList(
                    new Person("Jacob", "Smith"),
                    new Person("Isabella", "Johnson"),
                    new Person("Ethan", "Williams"),
                    new Person("Emma", "Jones"),
                    new Person("Michael", "Brown")
            );

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

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(300);
        stage.setHeight(1000);

        TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setPrefWidth(100);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));

        TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setPrefWidth(100);
        lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol);

        table2.setItems(data);
        table2.getColumns().addAll(firstNameCol, lastNameCol);

        ScrollPane sp = new ScrollPane(table);
        sp.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
        sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

        ScrollPane sp2 = new ScrollPane(table2);
        sp2.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
        sp2.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

        sp.setHmax(3);
        sp2.setHmax(3);
        sp.hvalueProperty().bindBidirectional(sp2.hvalueProperty());

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(sp, sp2);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;

        private Person(String fName, String lName) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

可以用这种方式绑定滚动条:

void bindScrollBars(TableView<?> firstTable, TableView<?> secondTable, Orienatation orieantation){
    ScrollBar firstScrollBar = null;
    ScrollBar secondScrollBar = null;

    for (Node node : firstTable.lookupAll(".scroll-bar")) {
            if (node instanceof ScrollBar && ((ScrollBar) node).getOrientation().equals(orientation)) {
                firstScrollBar= (ScrollBar) node;
            }
    }

    for (Node node : secondTable.lookupAll(".scroll-bar")) {
            if (node instanceof ScrollBar && ((ScrollBar) node).getOrientation().equals(orientation)) {
               secondScrollBar = (ScrollBar) node;
            }
     }

    if(firstScrollBar != null && secondScrollBar != null) { firstScrollBar.valueProperty().bindBidirectional(secondScrollBar.valueProperty()); }

对我来说它工作正常,你可以水平和垂直地绑定它。

答案 1 :(得分:1)

您的第一个问题是VBox无法正确缩放场景,这意味着您的ScrollPane永远不会小于其内容。你可以通过改变这个来解决这个问题:

((Group) scene.getRoot()).getChildren().addAll(vbox);

到此:

Scene scene = new Scene(vbox);

您还必须在start开头删除之前的声明:

Scene scene = new Scene(new Group()); //Remove this line

现在你的ScrollPane可以适当地缩放你的场景。但是,您仍然需要绑定它们的vvalue以便它们垂直匹配。在下面的行中绑定hvalue s:

sp.hvalueProperty().bindBidirectional(sp2.hvalueProperty());
sp.vvalueProperty().bindBidirectional(sp2.vvalueProperty()); //Add this line

现在你的滚动工作正常:

working scrollbars