如何在 VBox (JavaFX) 中删除 Table 上的滚动条?

How To Remove Scrollbar On Table Within a VBox (JavaFX)?

我在 VBox 中有一个 TableView,TableView 上有一个滚动条,我无法摆脱它,这真令人沮丧。任何帮助删除此内容的帮助将不胜感激。

基本上我在 Vbox 中有 TableView,因为还有一个我想放在 TableView 上方的标签。任何也能实现这一点的替代建议都很棒。

代码:

private void setFlagTab(Tab FlagTab, String name, ArrayList<Flag> flagList){

TableView table = new TableView();

ObservableList<Flag> data = FXCollections.observableArrayList(flagList);


Label label = new Label("Flags identified for: " + name);
label.setFont(new Font("Arial", 20));


TableColumn startCol = new TableColumn("Start Time");
startCol.setCellValueFactory(
        new PropertyValueFactory<Flag, String>("startTime"));

startCol.setSortable(false);
startCol.prefWidthProperty().bind(table.widthProperty().multiply(0.1));

TableColumn endCol = new TableColumn("End Time");
endCol.setCellValueFactory(
        new PropertyValueFactory<Flag, String>("endTime"));

endCol.setSortable(false);
endCol.prefWidthProperty().bind(table.widthProperty().multiply(0.1));

TableColumn phaseCol = new TableColumn("Phase");
phaseCol.setCellValueFactory(
        new PropertyValueFactory<Flag, String>("phase"));

phaseCol.setSortable(false);
phaseCol.prefWidthProperty().bind(table.widthProperty().multiply(0.2));

TableColumn messageCol = new TableColumn("Message");
messageCol.setCellValueFactory(
        new PropertyValueFactory<Flag, String>("message"));

messageCol.setSortable(false);
messageCol.prefWidthProperty().bind(table.widthProperty().multiply(0.4));

TableColumn targetCol = new TableColumn("Target");
targetCol.setCellValueFactory(
        new PropertyValueFactory<Flag, String>("target"));

targetCol.setSortable(false);
targetCol.prefWidthProperty().bind(table.widthProperty().multiply(0.1));

TableColumn actualCol = new TableColumn("Actual");
actualCol.setCellValueFactory(
        new PropertyValueFactory<Flag, String>("actual"));

actualCol.setSortable(false);
actualCol.prefWidthProperty().bind(table.widthProperty().multiply(0.1));



table.setItems(data);
table.getColumns().addAll(startCol, endCol, phaseCol, messageCol, targetCol, actualCol);


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


    FlagTab.setText("Flags");
    FlagTab.setContent(vbox);
}

谢谢!

考虑以下示例:

TableView table = new TableView();

TableColumn nameCol = new TableColumn("Name");
nameCol.setCellValueFactory(new PropertyValueFactory("name"));
nameCol.prefWidthProperty().bind(table.widthProperty().multiply(0.4));

TableColumn surnameCol = new TableColumn("Surname");
surnameCol.setCellValueFactory(new PropertyValueFactory("surname"));
    surnameCol.prefWidthProperty().bind(table.widthProperty().multiply(0.4));

TableColumn ageCol = new TableColumn("Age");
ageCol.setCellValueFactory(new PropertyValueFactory("age"));
ageCol.prefWidthProperty().bind(table.widthProperty().multiply(0.2));

table.getColumns().addAll(nameCol, surnameCol, ageCol);

StackPane root = new StackPane();
root.getChildren().add(table);

Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();

// Uncomment this line to see the scrollbar
// table.getItems().add(new Person("John", "Doe", 26));

如果你按原样运行它会工作正常,但看起来很奇怪,一旦你向 table 添加一些数据,水平滚动条就会出现。我知道有两种解决方法。

  1. 将列绑定到 table 宽度时,请确保保留一些额外的 space,例如将 multiply(0.4) 替换为 multiply(0.39)
  2. 如果您不想调整列宽,并且您觉得您永远不需要水平滚动条,您可以通过设置此列调整大小策略来完全禁用它: table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);