我有一个TableView
,它会随着窗口大小的调整而正确缩小/增长。
但是,我希望其中一列也自动增长。与 HGrow
属性在其他节点上的工作方式类似。
TableColumn
似乎没有任何类型的 HGrow
或类似属性来告诉该列使用所有可用空间。
我确实尝试将 PrefWidth
属性设置为 Double.MAX_VALUE
,但导致列无限期扩展。
我的目标是让 TableView
适合窗口,并且如果调整窗口大小,而不是扩展最后一个伪列(实际上不是列) ),它将扩展首选列。
这是我的 MCVE:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableColumnGrow extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple Interface
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
TableView<Person> tableView = new TableView<>();
TableColumn<Person, String> colFirstName = new TableColumn<>("First Name");
TableColumn<Person, String> colLastName = new TableColumn<>("Last Name");
colFirstName.setCellValueFactory(tf -> tf.getValue().firstNameProperty());
colLastName.setCellValueFactory(tf -> tf.getValue().lastNameProperty());
tableView.getColumns().addAll(colFirstName, colLastName);
tableView.getItems().addAll(
new Person("Martin", "Brody"),
new Person("Matt", "Hooper"),
new Person("Sam", "Quint")
);
root.getChildren().add(tableView);
// Show the stage
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("TableColumnGrow Sample");
primaryStage.show();
}
}
class Person {
private final StringProperty firstName = new SimpleStringProperty();
private final 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 void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
}
<小时/>
因此,我们的目标是删除红色列,并让“名字”列占据额外的空间(而不将“姓氏”推到界限之外)。
这是否可以在不诉诸昂贵的宽度计算的情况下实现?
编辑:虽然我的示例确实使用了 TableView
中的第一列,但我希望它适用于我选择的任何列,无论有多少列TableView
有。
最佳答案
我想我应该尝试一下。我基本上是在监听列宽何时变化以及表格宽度何时变化。
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.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXTestingGround extends Application{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple Interface
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
TableView<Person> tableView = new TableView<>();
TableColumn<Person, String> colFirstName = new TableColumn<>("First Name");
TableColumn<Person, String> colMiddleName = new TableColumn<>("Last Name");
TableColumn<Person, String> colLastName = new TableColumn<>("Last Name");
colFirstName.setCellValueFactory(tf -> tf.getValue().firstNameProperty());
colLastName.setCellValueFactory(tf -> tf.getValue().lastNameProperty());
colLastName.setCellValueFactory(tf -> tf.getValue().lastNameProperty());
tableView.getColumns().addAll(colFirstName, colMiddleName, colLastName);
tableView.getItems().addAll(
new Person("Martin", "One", "Brody"),
new Person("Matt", "Two", "Hooper"),
new Person("Sam", "Three", "Quint")
);
root.getChildren().add(tableView);
// Show the stage
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("TableColumnGrow Sample");
primaryStage.show();
DoubleProperty width = new SimpleDoubleProperty();
width.bind(colMiddleName.widthProperty().add(colLastName.widthProperty()));
width.addListener((ov, t, t1) -> {
colFirstName.setPrefWidth(tableView.getWidth() - 5 - t1.doubleValue());
});
colFirstName.setPrefWidth(tableView.getWidth() - 5 - width.doubleValue());
colFirstName.setResizable(false);
tableView.widthProperty().addListener((ov, t, t1) -> {
colFirstName.setPrefWidth(tableView.getWidth() - 5 - width.doubleValue());
});
}
}
class Person {
private final StringProperty firstName = new SimpleStringProperty();
private final StringProperty lastName = new SimpleStringProperty();
private final StringProperty middleName = new SimpleStringProperty();
public Person(String firstName, String middleName, String lastName) {
this.firstName.set(firstName);
this.middleName.set(middleName);
this.lastName.set(lastName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getMiddleName() {
return lastName.get();
}
public void setMiddleName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty middleNameProperty() {
return lastName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
}
关于javafx - 如何设置一个TableColumn随TableView一起增长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57245216/