javafx - 如何设置一个TableColumn随TableView一起增长?

标签 javafx

我有一个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;
    }
}
<小时/>

screenshot

因此,我们的目标是删除红色列,并让“名字”列占据额外的空间(而不将“姓氏”推到界限之外)。

这是否可以在不诉诸昂贵的宽度计算的情况下实现?

编辑:虽然我的示例确实使用了 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/

相关文章:

css - JavaFX 如何在 tableView 中隐藏空列单元格

focus - JavaFX:使用箭头键滚动与焦点遍历

java - 如何让 NetBeans 在 src 文件夹而不是 dist 文件夹中搜索我的文件?

java - 为什么 JavaFX API 不包含在 Java 8 J2SE 中?

在 IntelliJ 运行时异常上使用 Maven 的 JavaFX 项目

JavaFX 11 : IllegalAccessError when creating Label

java - 将 ComboBox#itemsProperty 绑定(bind)到 Map#keySet

java - 如何在 javaFx java8 中禁用 textArea 自动滚动

java - 在 JavaFX 桌面应用程序中获取远程 IP 地址

java - 无法在 BorderPane 中 Pos.CENTER