javajavafxresizetableviewscrollbar

How to set horizontal scrollbar for TableView?


I have an issue with my tableView. My tableview has two columns and rows inside second one are file paths. So i would like to implement scrollbar to see whole path by scrolling, because tableView size is fixed.

I've added setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY); It shows scrollbar, but i cant move it.

Is there any functional solution?

enter image description here


Solution

  • Since it isn't possible to upload an image into a comment I'm posting one in an answer. I just want to make sure you knew what I meant in my comment. If you did and/or this doesn't help you, let me know and I'll delete this answer.

    This is what I meant by increasing the column width via the UI. As shown in the GIF, I did have to scroll a little to the right before I was able to resize the second column. This is probably because there are like 1 or 2 pixels "off screen" when the table is first rendered.

    enter image description here

    Here is the code I used to create this example (Java 10):

    public class TestApplication extends Application {
    
        @Override
        @SuppressWarnings("unchecked")
        public void start(Stage primaryStage) throws Exception {
            var table = new TableView<>(FXCollections.observableArrayList(
                    new Person("John", "SomeGuyWithAReallyObnoxiouslyLongLastName"),
                    new Person("Jane", "Smith")
            ));
    
            var firstNameCol = new TableColumn<Person, String>("First Name");
            firstNameCol.setCellValueFactory(v -> v.getValue().firstNameProperty());
    
            var lastNameCol = new TableColumn<Person, String>("Last Name");
            lastNameCol.setCellValueFactory(v -> v.getValue().lastNameProperty());
    
            table.getColumns().addAll(firstNameCol, lastNameCol);
            table.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
            primaryStage.setOnShown(we -> {
                we.consume();
                firstNameCol.setPrefWidth(table.getWidth() / 2.0);
                lastNameCol.setPrefWidth(table.getWidth() / 2.0);
            });
    
            var root = new StackPane(table);
            root.setPadding(new Insets(30.0));
            var scene = new Scene(root, 560.0, 360.0);
            primaryStage.setScene(scene);
            primaryStage.setTitle("People");
            // primaryStage.setResizable(false);
            primaryStage.show();
        }
    
        public static class Person {
    
            private final StringProperty firstName = new SimpleStringProperty(this, "firstName");
            public final void setFirstName(String name) { firstName.set(name); }
            public final String getFirstName() { return firstName.get(); }
            public final StringProperty firstNameProperty() { return firstName; }
    
            private final StringProperty lastName = new SimpleStringProperty(this, "lastName");
            public final void setLastName(String name) { lastName.set(name); }
            public final String getLastName() { return lastName.get(); }
            public final StringProperty lastNameProperty() { return lastName; }
    
            public Person(String firstName, String lastName) {
                setFirstName(firstName);
                setLastName(lastName);
            }
    
        }
    
    }