user38725
user38725

Reputation: 903

Completely Hiding or Disabling Horizontal Scrollbar in JavaFX TableView

I realize there are other posts about this, but this is not a duplicate.

If I use the following code to hide the horizontal scrollbar from a TableView, there still is 1 or 2 pixel area at the bottom of the table, from which the user can scroll the TableView horizontally --- with the mouse wheel (SHIFT+mouse wheel also works, from anywhere in the table area).

.table-view *.scroll-bar:horizontal *.increment-button,
.table-view *.scroll-bar:horizontal *.decrement-button {
    -fx-background-color: null;
    -fx-background-radius: 0;
    -fx-background-insets: 0;
    -fx-padding: 0;
}

.table-view *.scroll-bar:horizontal *.increment-arrow,
.table-view *.scroll-bar:horizontal *.decrement-arrow {
    -fx-background-color: null;
    -fx-background-radius: 0;
    -fx-background-insets: 0;
    -fx-padding: 0;
    -fx-shape: null;
}

I tried some reflection hacks to access the scrollbar and attempt to disable it that way, with no success. Anyone have ideas?

EDIT - here's a minimal example, for me it's always reproducible on Win 10, JavaFX 12.0.1.

Test.java:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * Horizontal scrolling via mouse wheel is possible when you place the cursor at the bottom of the window, just above the resize-area.
 */
public class Test extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<String> table = new TableView<>();

        table.getColumns().add(new TableColumn<>("first"));
        table.getColumns().add(new TableColumn<>("second"));
        table.getColumns().add(new TableColumn<>("third"));
        table.getColumns().add(new TableColumn<>("fourth"));
        table.getColumns().add(new TableColumn<>("fifth"));
        table.getColumns().add(new TableColumn<>("sixth"));
        table.getColumns().add(new TableColumn<>("seventh"));
        table.getColumns().add(new TableColumn<>("eighth"));
        table.getColumns().add(new TableColumn<>("ninth"));
        table.getColumns().add(new TableColumn<>("tenth"));

        table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

//        table.addEventFilter(ScrollEvent.ANY, Event::consume);

        ObservableList<String> data = FXCollections.observableArrayList();
        for (int i = 0; i < 5000; i++) {
            data.add("foobar");
        }
        table.setItems(data);

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

        Scene scene = new Scene(root, 300, 250);

        scene.getStylesheets().add("test.css");

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(Test.class);
    }
}

Test.CSS:

.table-view .scroll-bar * {
    -fx-min-width: 0;
    -fx-pref-width: 0;
    -fx-max-width: 0;

    -fx-min-height: 0;
    -fx-pref-height: 0;
    -fx-max-height: 0;
}

Upvotes: 2

Views: 2684

Answers (1)

Sunflame
Sunflame

Reputation: 3186

If you don't want to go for the css solution then you can do this way

public static <T extends Control> void removeScrollBar(T table) {
        ScrollBar scrollBar = (ScrollBar) table.queryAccessibleAttribute(AccessibleAttribute.HORIZONTAL_SCROLLBAR);
        /*
         *This null-check is for safety reasons if you are using when the table's skin isn't yet initialized.
         * If you use this method in a custom skin you wrote, where you @Override the layoutChildren method,
         * use it there, and it should be always initialized, so null-check would be unnecessary.
         *
         */
        if (scrollBar != null) {
            scrollBar.setPrefHeight(0);
            scrollBar.setMaxHeight(0);
            scrollBar.setOpacity(1);
            scrollBar.setVisible(false); // If you want to keep the scrolling functionality then delete this row.
        }
    }

Upvotes: 3

Related Questions