Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent TableView from doing TableColumn re-order in javaFX 8?

I read this https://bugs.openjdk.java.net/browse/JDK-8102128 but I haven't found something in the Api of JavaFX 8.There isn't any way of prevent reordering in TableView?

like image 275
geo Avatar asked Mar 05 '14 15:03

geo


2 Answers

Expanding on a previous contribution I found this

tableView.getColumns().forEach(e -> e.setReorderable(false));

to be very useful in Java 13.

like image 173
Vincentklw Avatar answered Sep 21 '22 09:09

Vincentklw


Spent half of a day trying to solve the problem. Maybe my investigation can be useful for someone else. It's possible to disable column reordering using some hacks. And here're the steps:

  1. Get table header. This can be done as described here: https://stackoverflow.com/a/12465155
  2. Add change event listener to reorderingProperty which sets reordering back to false.

Full code is here:

tableView.widthProperty().addListener(new ChangeListener<Number>()
{
    @Override
    public void changed(ObservableValue<? extends Number> source, Number oldWidth, Number newWidth)
    {
        TableHeaderRow header = (TableHeaderRow) tableView.lookup("TableHeaderRow");
        header.reorderingProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                header.setReordering(false);
            }
        });
    }
});

I'm not sure about side effects of this solution, but quick tests show that solution works well.

like image 32
Alexander Chingarev Avatar answered Sep 21 '22 09:09

Alexander Chingarev