我在VBox中有一个TableView,并且有一个非常令人沮丧的滚动条,我无法在TableView上摆脱它。任何帮助删除这一点将不胜感激。
基本上我在Vbox中有TableView,因为还有一个我想要放在TableView上面的Label。任何可以实现这一目标的替代建议都很棒。
代码:
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);
}
谢谢!
答案 0 :(得分:0)
考虑以下示例:
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));
如果按原样运行它会正常工作,但看起来很奇怪,一旦你向表中添加一些数据,就会出现水平滚动条。我知道有两种解决方法。
multiply(0.4)
替换为multiply(0.39)
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);