2 Replies Latest reply: Nov 29, 2012 8:57 AM by qwerty1 RSS

    Table sorting

    qwerty1
      I have a problem with table sorting. I use jdk 8 build 63.

      Say column contains unsorted data (simple strings for simplicity)
      I click once on header and my column is sorted in ascending order with the ascending arrow,
      I click again, column is sorted in descending order with the descending arrow,
      I click again, arrow disappears but sort order is not restored to the original unsorted order ! Looks like a bug.

      I expected either
      1.sort would cycle through ascending/descending order never being able to have unsorted state like TableRowSorter in java
      or
      2 sort would support unsorted state with the third click bringing column back to that state

      but what I see is something in between...

      Anyone knows how to fix it? Or is it "as designed" ??

      Thanks !
        • 1. Re: Table sorting
          ytw
          I believe the old behavior was #2 (ascend, descend, unsort) with Jdk1.7.0_06 on Mac OS X.

          One way to achieve #1 is to monitor the 'Sort Order' list of a TableView (TableView.getSortOrder()).
          Basically, the idea is to save the 'sort column' when one is set and restore it when 'Sort Order' list becomes empty.
          tableView.getSortOrder().addListener(new ListChangeListener<TableColumn<MyObject, ?>>() {
              @Override
              public void onChanged(final javafx.collections.ListChangeListener.Change<? extends TableColumn<MyObject, ?>> arg0) {
                      if (tableView.getSortOrder().size() > 0) {
                          // save the current sort column some where so we can't add it back later when tableView.getSortOrder().size() is 0.
                      }
                      else {
                          // add a sort column back and set its sort type (ascending/descending)
                      }
              }
          });
          • 2. Re: Table sorting
            qwerty1
            Thanks ytw! It's a workaround for sure. I was hoping that maybe table supports true unsorted state instead !