简体   繁体   中英

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

没有水平滚动条出现

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM