【问题标题】:how to make a table visible with custom rows & column如何使用自定义行和列使表格可见
【发布时间】:2014-12-04 01:10:28
【问题描述】:

我有以下代码来创建自定义表格。但在输出中它显示了许多不包含任何值的行。我想只显示两行和一列。是否有任何解决方案,否则 Javafx 默认会生成此问题。有没有其他方法可以创建表。可能正在使用 GridPaneBuilder

private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data =
    FXCollections.observableArrayList(
        new Person("Jacob"),
        new Person("Isabella")
    );

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(450);
    stage.setHeight(500);

    final Label label = new Label("Address Book");
    label.setFont(new Font("Arial", 20));

    table.setEditable(true);

    TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setMinWidth(100);
    firstNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("firstName"));

    table.setItems(data);
    table.getColumns().addAll(firstNameCol);

    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10, 0, 0, 10));
    vbox.getChildren().addAll(label, table);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene);
    stage.show();
}

public static class Person {

    private final SimpleStringProperty firstName;

    private Person(String fName) {
        this.firstName = new SimpleStringProperty(fName);

    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String fName) {
        firstName.set(fName);
    }
}

【问题讨论】:

  • 你的意思是你想让空行不被着色吗?还是您希望表格高度与行的高度一致?如果您只想更改空行的颜色,请看这里:fxexperience.com/2011/11/…
  • 我希望重新调整我的桌子大小。我只需要 2 行。只是列名和 2 行。

标签: javafx javafx-2 javafx-8


【解决方案1】:

您可以通过以下方式设置列占用尽可能多的空间:

myTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

我不知道是否有一种简单的方法可以根据行数设置表格的高度,但是您可以根据行数乘以rowHeight来设置表格的maxHeight:

myTable.setMaxHeight(countOfRows * rowHeight + headerHeight);

更灵活的方法是使用 JavaFX 绑定,因此当您添加或删除一行时,表格的高度会发生变化。

【讨论】:

    猜你喜欢
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多