Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Save and restore visual state of TableView (order, width and visibility)

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);
  });
}
like image 590
user1438038 Avatar asked Nov 28 '14 12:11

user1438038


1 Answers

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.

like image 114
schw4ndi Avatar answered Oct 22 '22 10:10

schw4ndi