这是因为您在表视图中包含了 CONSTRAINED_RESIZE_POLICY。
java 文档说::
确保所有可见叶列的宽度的简单策略
这个表格的总和等于表格本身的宽度。
当用户使用此策略调整列宽时,表格
自动调整右侧列的宽度。什么时候
用户增加一列宽度,表格减小宽度
最右边的列,直到它达到其最小宽度。然后它
减小最右边第二列的宽度,直到达到
最小宽度等等。当所有右侧列到达
最小大小,用户不能增加任何调整大小的列的大小
更多。
话虽如此,如果您故意包含 CONSTRAINED_RESIZE_POLICY,那么您可能需要添加更多自定义逻辑来满足自定义宽度以及策略。
更新:
如果您想在 GridPane 中实现类似“percentWidth”功能的功能,您可以尝试以下方法。
想法是:
- 创建具有新属性“percentWidth”的自定义 TableColumn
- 创建一个自定义 TableView,它有一个对其 widthProperty 的侦听器,并根据 percentWidth 调整列 prefWidth。
- 在 fxml 中导入这些控件并使用新控件更新 fxml。
CustomTableColumn.java
public class CustomTableColumn<S, T> extends TableColumn<S, T> {
private DoubleProperty percentWidth = new SimpleDoubleProperty();
public CustomTableColumn(String columnName) {
super(columnName);
}
public DoubleProperty percentWidth() {
return percentWidth;
}
public double getPercentWidth() {
return percentWidth.get();
}
public void setPercentWidth(double percentWidth) {
this.percentWidth.set(percentWidth);
}
}
CustomTableView.java
public class CustomTableView<S> extends TableView<S> {
public CustomTableView() {
widthProperty().addListener((obs, old, tableWidth) -> {
// Deduct 2px from the total table width for borders. Otherwise you will see a horizontal scroll bar.
double width = tableWidth.doubleValue() - 2;
getColumns().stream().filter(col -> col instanceof CustomTableColumn)
.map(col -> (CustomTableColumn) col)
.forEach(col -> col.setPrefWidth(width * (col.getPercentWidth() / 100)));
});
}
}
更新的 fxml 代码:
<CustomTableView fx:id="reportTableView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<columns>
<CustomTableColumn fx:id="date" percentWidth="35" text="Date" />
<CustomTableColumn fx:id="company" percentWidth="40" text="Company" />
<CustomTableColumn fx:id="number" percentWidth="25" text="Number" />
...
</columns>
</CustomTableView>
请注意,所有列的总和 percentWidth 应等于 100 以获得更好的结果:)
此实现的完整工作演示(非 fxml)如下:(我更新了代码以修复 gif 中的水平滚动条)
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class PercentageTableColumnDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Harry", "John", "LS"));
persons.add(new Person("Mary", "King", "MS"));
CustomTableColumn<Person, String> fnCol = new CustomTableColumn<>("First Name");
fnCol.setPercentWidth(30);
fnCol.setCellValueFactory(param -> param.getValue().firstNameProperty());
CustomTableColumn<Person, String> lnCol = new CustomTableColumn<>("Last Name");
lnCol.setPercentWidth(25);
lnCol.setCellValueFactory(param -> param.getValue().lastNameProperty());
CustomTableColumn<Person, String> cityCol = new CustomTableColumn<>("City");
cityCol.setPercentWidth(45);
cityCol.setCellValueFactory(param -> param.getValue().cityProperty());
CustomTableView<Person> tableView = new CustomTableView<>();
tableView.getColumns().addAll(fnCol, lnCol, cityCol);
tableView.setItems(persons);
VBox root = new VBox();
root.getChildren().addAll(tableView);
VBox.setVgrow(tableView, Priority.ALWAYS);
Scene scene = new Scene(root, 500, 500);
stage.setScene(scene);
stage.setTitle("Table demo");
stage.show();
}
class CustomTableColumn<S, T> extends TableColumn<S, T> {
private DoubleProperty percentWidth = new SimpleDoubleProperty();
public CustomTableColumn(String columnName) {
super(columnName);
}
public DoubleProperty percentWidth() {
return percentWidth;
}
public double getPercentWidth() {
return percentWidth.get();
}
public void setPercentWidth(double percentWidth) {
this.percentWidth.set(percentWidth);
}
}
class CustomTableView<S> extends TableView<S> {
public CustomTableView() {
widthProperty().addListener((obs, old, tableWidth) -> {
// Deduct 2px from the total table width for borders. Otherwise you will see a horizontal scroll bar.
double width = tableWidth.doubleValue() - 2;
getColumns().stream().filter(col -> col instanceof CustomTableColumn)
.map(col -> (CustomTableColumn) col)
.forEach(col -> col.setPrefWidth(width * (col.getPercentWidth() / 100)));
});
}
}
class Person {
private StringProperty firstName = new SimpleStringProperty();
private StringProperty lastName = new SimpleStringProperty();
private StringProperty city = new SimpleStringProperty();
public Person(String fn, String ln, String cty) {
setFirstName(fn);
setLastName(ln);
setCity(cty);
}
public String getFirstName() {
return firstName.get();
}
public StringProperty firstNameProperty() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public String getLastName() {
return lastName.get();
}
public StringProperty lastNameProperty() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public String getCity() {
return city.get();
}
public StringProperty cityProperty() {
return city;
}
public void setCity(String city) {
this.city.set(city);
}
}
}