Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8125017

[TableColumn] newly set comparator is not applied immidiately

XMLWordPrintable

      Look at the attached movie. I applied new comparator (it is reversed). It was applied, only when I changed sort order.

      Application:

      import java.util.Comparator;
      import javafx.application.Application;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.value.ObservableValue;
      import javafx.event.Event;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.util.Callback;

      public class JavaApplication61 extends Application {

          public static void main(String[] args) {
              launch(args);
          }

          @Override
          public void start(Stage stage) throws Exception {
              VBox vb = new VBox();
              TableView tv = new TableView();
              for (int i = 0; i < 10; i++) {
                  tv.getItems().add(new Data("Item-" + String.valueOf(i)));
              }

              final TableColumn tc = new TableColumn();
              tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data, String>, ObservableValue<String>>() {

                  public ObservableValue<String> call(TableColumn.CellDataFeatures<Data, String> p) {
                      return new SimpleStringProperty(p.getValue().toString());
                  }
              });

              tv.getColumns().add(tc);

              Button b = new Button("set comparator");
              b.setOnAction(new EventHandler() {

                  @Override
                  public void handle(Event t) {
                      tc.setComparator(new Comparator() {

                          @Override
                          public int compare(Object t, Object t1) {
                              return -t.toString().compareTo(t1.toString());
                          }
                      });
                  }
              });
              vb.getChildren().addAll(tv, b);

              Scene scene = new Scene(vb, 300, 500);
              stage.setScene(scene);
              stage.show();
          }

          private class Data {

              public String data;

              public Data(String data) {
                  this.data = data;
              }

              @Override
              public String toString() {
                  return data;
              }
          }
      }

            jgiles Jonathan Giles
            akirov Alexander Kirov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: