Search code examples
javafxtableviewscrollbarvbox

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


I have a TableView within a VBox and there is a really frustrating scrollbar that I can't get rid of on the TableView. Any help removing this would be appreciated.

Essentially I have the TableView within a Vbox because there is also a Label which I want to sit above the TableView. Any alternative suggestions that also achieve this are great.

Code:

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);
}

Thanks!


Solution

  • Consider the following example:

    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));
    

    It will work fine if you run it as it is, but strange as it seems, once you add some data to the table the horizontal scrollbar will be appear. There are two workarounds that I know of.

    1. When binding columns to the table width, make sure to leave some additional space, for example by replacing multiply(0.4) with multiply(0.39)
    2. If you don't want to meddle with columns widths, and you feel that you will never need the horizontal scrollbar, you can disable it completely by setting this column resize policy: table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);