JavaFx创建一个包含两个表格和两个滚动条的新布局实现固定列
时间: 2024-03-11 15:48:33 浏览: 107
要现固定列的布局,可以使用JavaFX的SplitPane组件,将两个表格分别放在左右两个Pane中,然后再将两个Pane放在SplitPane中。对于固定列,可以将左侧表格的列设置为不可滚动,右侧表格的列设置为可滚动。同时,将两个表格的滚动条绑定起来,使它们能够同步滚动。
下面是一个简单的代码示例:
```java
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class FixedColumnLayout extends Application {
private TableView<Person> leftTable;
private TableView<Person> rightTable;
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("John", "Doe"),
new Person("Jane", "Doe"),
new Person("Bob", "Smith"),
new Person("Mike", "Johnson"),
new Person("Alice", "Jones")
);
@Override
public void start(Stage primaryStage) {
leftTable = createTable();
leftTable.setFixedCellSize(25);
leftTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
leftTable.getColumns().get(0).setResizable(false);
rightTable = createTable();
rightTable.setFixedCellSize(25);
rightTable.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
ScrollBar leftScrollBar = getScrollBar(leftTable);
ScrollBar rightScrollBar = getScrollBar(rightTable);
leftScrollBar.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
rightScrollBar.setValue(newValue.doubleValue());
}
});
rightScrollBar.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
leftScrollBar.setValue(newValue.doubleValue());
}
});
SplitPane splitPane = new SplitPane();
splitPane.setOrientation(Orientation.HORIZONTAL);
splitPane.getItems().addAll(leftTable, rightTable);
BorderPane root = new BorderPane();
root.setCenter(splitPane);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private TableView<Person> createTable() {
TableView<Person> table = new TableView<>();
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
firstNameCol.setPrefWidth(100);
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
lastNameCol.setPrefWidth(100);
table.getColumns().addAll(firstNameCol, lastNameCol);
table.setItems(data);
return table;
}
private ScrollBar getScrollBar(TableView<?> table) {
for (Object node : table.lookupAll(".scroll-bar")) {
if (node instanceof ScrollBar) {
ScrollBar scrollBar = (ScrollBar) node;
if (scrollBar.getOrientation() == Orientation.VERTICAL) {
return scrollBar;
}
}
}
return null;
}
public static void main(String[] args) {
launch(args);
}
public static class Person {
private final String firstName;
private final String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
}
```
在这个示例中,我们创建了两个表格,将左侧表格的第一列设置为不可滚动,右侧表格的第一列可滚动。然后,我们获取了两个表格的滚动条,并将它们绑定起来,以便它们能够同步滚动。最后,我们将两个表格放在SplitPane中,并将SplitPane放在BorderPane的中央。
阅读全文