In a JavaFX TableView, how can I determine changes of
- I. The column order [solved]
- II. The column's width [solved]
- III. The column's visibilty [solved]
to save them in preferences and restore them, the next time I start an application?
I. The column order
Works now. However, wasRemoved()
is triggered when reordering columns, not wasPermutation()
.
final List<TableColumn<MyType, ?>> unchangedColumns = Collections.unmodifiableList(new ArrayList<TableColumn<MyType, ?>>(columns));
columns.addListener(new ListChangeListener<TableColumn<MyType, ?>>() {
@Override
public void onChanged(ListChangeListener.Change<? extends TableColumn<MyType, ?>> change) {
while (change.next()) {
if (change.wasRemoved()) {
ObservableList<TableColumn<MyType, ?>> columns = table.getColumns();
int[] colOrder = new int[columns.size()];
for (int i = 0; i < columns.size(); ++i) {
colOrder[i] = unchangedColumns.indexOf(columns.get(i));
}
// colOrder will now contain current order (e.g. 1, 2, 0, 5, 4)
}
}
}
});
II. The column's width
This is working.
for (TableColumn<MyType, ?> column: columns) {
column.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldWidth, Number newWidth) {
logger.info("Width: " + oldWidth + " -> " + newWidth);
});
}
III. The column's visibilty
This does the trick.
for (TableColumn<MyType, ?> column: columns) {
column.visibleProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observableValue, Boolean oldVisibility, Boolean newVisibility) {
logger.info("Visibility: " + oldVisibility + " -> " + newVisibility);
});
}
column.visibleProperty()
? – Ukrainian