In a JavaFX TableView, how can I determine changes of
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);
});
}
I have implemented a simple custom TableView as an extension of the TableView for automated storage of the columns order and width. It is based on the given solution in the question. My custom TableView is called PropertySaveTableView.
You can find it here: https://gist.github.com/y4nnick/ca976e58be23aab20dfbc8d81ea46816
The "EinstellungService.java" is only a interface for permanent storage of the properties. You must create your own implementation with you favorite technology.
Maybe i will add the visibility saving later, if so i will let you know here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With