gpt4 book ai didi

JavaFX 表格 View : remove/disable scollbars from the table(using a ScrollPane instead)

转载 作者:行者123 更新时间:2023-12-02 02:44:02 25 4
gpt4 key购买 nike

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

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

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

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

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

最佳答案

可以通过这种方式绑定(bind)滚动条:

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

对我来说它工作得很好,你可以水平和垂直绑定(bind)它。

关于JavaFX 表格 View : remove/disable scollbars from the table(using a ScrollPane instead),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44942059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com