JavaFX: saving and restoring the visual state of a TableView (order, width, and visibility)

In JavaView TableView, how can I spot changes

  • I. Column order [SOLVED]
  • II. Column Width [Solved]
  • III. Visibilty column [SOLVED]

to save them in the settings and restore them the next time you start the application?

I. Column Order

It works now. However, wasRemoved() fired when the columns are reordered, 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 (eg 1, 2, 0, 5, 4) } } } }); 

II. Column Width

It works.

 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. Column designation

This is a 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); }); } 
+5
source share
2 answers

I have implemented a simple custom TableView as a TableView extension to automatically store the order and width of columns. It is based on this solution in question. My custom TableView is called PropertySaveTableView.

You can find it here: https://gist.github.com/y4nnick/ca976e58be23aab20dfbc8d81ea46816

"EinstellungService.java" is just an interface for persistent storage of properties. You must create your own implementation with your favorite technology.

Perhaps I will add maintaining visibility later, if so, I will tell you here.

+1
source

FWIW - The following code is in addition to the first part of the question to apply the saved column order:

 private void applyColumnOrder(int[] order) { ObservableList<TableColumn<MyType, ?>> columns = getColumns(); if (order.length == columns.size()) { columns.clear(); for (int ix = 0; ix < order.length; ix++) { columns.add(unchangedColumns.get(order[ix])); } } } 

At the top, here are some methods to preserve the sort order of tables:

 private void saveSortOrder(ArrayList<String> sortCols, ArrayList<String> sortDirs) { sortCols = new ArrayList<>(); sortDirs = new ArrayList<>(); for (TableColumn<MyType, ?> c : getSortOrder()) { sortCols.add(c.getText()); sortDirs.add(c.getSortType().toString()); } } private void applySortOrder(ArrayList<String> sortCols, ArrayList<String> sortDirs) { List<TableColumn<MyType, ?>> sortOrder = getSortOrder(); sortOrder.clear(); for (int ix = 0; ix < sortCols.length; ix++) { for (TableColumn<MyType, ?> c : unchangedColumns) { if (c.getText().equals(sortCols[ix])) { sortOrder.add(c); c.setSortType(TableColumn.SortType.valueOf(sortDirs[ix])); } } } } 
0
source

Source: https://habr.com/ru/post/1207888/


All Articles