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

geo picture geo · Mar 5, 2014 · Viewed 11.6k times · Source

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?

Answer

Alexander Chingarev picture Alexander Chingarev · May 20, 2015

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.