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

I read this https://bugs.openjdk.java.net/browse/JDK-8102128 , but I did not find something in Api JavaFX 8. Is there no way to prevent reordering in TableView?

+8
order javafx-8 tableview
source share
5 answers

Spent half a day trying to solve the problem. Maybe my investigation might be useful to someone else. You can disable column reordering with some hacks. And here are the steps:

  • Get the table title. This can be done as described here: stack overflow
  • Add a change event listener to reorderingProperty that sets the reordering back to false.

The 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 the side effects of this solution, but quick tests show that the solution works well.

+12
source share

I like the idea from Alexander Chingarev, but I think the code will leak a memory! Each time the width property changes, a new listener is registered, and older listeners never collect garbage! Thus, you can either save the link to the listener, but also remove it from the observed value (in this case, this is a reordering property) before adding a new listener or make sure that the listener is added only once to the reordering property.

I used the skin property, which changes only once (if I'm not mistaken) to add my listener:

 tableView.skinProperty().addListener((obs, oldSkin, newSkin) -> { final TableHeaderRow header = (TableHeaderRow) lookup("TableHeaderRow"); header.reorderingProperty().addListener((o, oldVal, newVal) -> header.setReordering(false)); }); 
+4
source share

API added in Java 8 RT-24669, column.impl_setReorderable(false); .

You can define the definition of impl_setReorderable in the source of TableColumnBase.java .

However, this API is for internal use only, marked as deprecated and not part of the public JavaFX API.

In general, the impl_ methods in JavaFX will be removed in the future in the future, potentially breaking your code at the time if you try to use them.

When viewing code to implement an overridable property in table columns, it works by ignoring some mouse events directed to the table column header. Find TableColumnHeader.java code for isReorderable for more information.

I'm not sure how you would accomplish the same behavior as the impl_setReorderable API, using only the open JavaFX API, without doing quite a bit of work.

+3
source share

Thanks! It also works with lambda if you have several tables that you want to disable.

 private void yourTableListeners(){ yourTableView.widthProperty() .addListener((observable, oldValue, newValue) -> yourReusableDisablingMethod(yourTableView)); anotherTableView.widthProperty() .addListener((observable, oldValue, newValue) -> yourReusableDisablingMethod(anotherTableView)); } @SuppressWarnings("restriction") private void yourReusableDisablingMethod(TableView tableView) { TableHeaderRow header = (TableHeaderRow) tableView.lookup("TableHeaderRow"); header.reorderingProperty().addListener(new ChangeListener<Boolean>() { public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { header.setReordering(false); } }); } 
+1
source share

I know the issue with java-8 tags, but for those wandering, In Java 9 all of the above codes will break due to the modularity that makes the .sun package inaccessible and remove impl_ . Despite these changes, he introduces convenient public methods that you can use:

  • setReorderable(boolean value)
  • getReorderable()

for a TableColumnBase , such as the TableColumn to be used for set Reorderable,

+1
source share

All Articles