Impulse The Fox
Impulse The Fox

Reputation: 2771

JavaFX 8 Tableview with horizontal scrolling

How can I make a horizontal scrolling bar appear instead of squishing all the columns to the extreme minimum size? You also should still be able to resize the columns unconstrained (and make the horizontal scroll bar adjust).

No horizontal scrolling bar appears

Upvotes: 1

Views: 11592

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36722

To make the column take their prefer size and not make them squeeze to available screen size, you can set the columnResizePolicy to UNCONSTRAINED_RESIZE_POLICY

tableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY); 

Since this is the default behaviour, you must have set columnResizePolicy to CONSTRAINED_RESIZE_POLICY somewhere.


Update

Here is a MCVE which shows how you can get a horizontal ScrollBar in a TableView. You can see that I do not set the columnResizePolicy because UNCONSTRAINED_RESIZE_POLICY is set by default.

Adding a MCVE for more clarity:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.stream.IntStream;

public class Test extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        TableView<Person> tableView = new TableView<>();
        tableView.setTableMenuButtonVisible(true);
        IntStream.range(0, 15).forEach(value -> {
            TableColumn<Person, String> tableColumn = new TableColumn<>("TableColumn" + value);
            tableColumn.setPrefWidth(100.0);
            tableView.getColumns().add(tableColumn);
        });

        IntStream.range(1, 10).forEach(value -> {
            Person person = new Person("Person" + value, value);
            tableView.getItems().add(person);
        });
        Scene scene = new Scene(new StackPane(tableView));
        stage.setScene(scene);
        stage.setWidth(500);
        stage.setHeight(500);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private class Person {
        private String name;
        private int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}

Upvotes: 4

Related Questions