Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

In JavaFX, binding comboBox items and table Column sort

DHLNov 3 2013 — edited Nov 4 2013

I have a combo Box and a table-view. ComboBox items are filled with tables column-names. I want to bind comboBox item selection and the table column sort. Example: If I select item say "Name" from comboBox which is at index 0 of comboBox, the 0th column of table is sorted. Again if I sort a column in the table, comboBox selected item should update with the corresponding column name. Right now i am achieving it using the below code.

private void bindComboBoxAndTableColumnSort() {

  topBarCombo.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {

       @Override

       public void changed(ObservableValue<? extends Number> arg0, Number oldVal, Number newVal) {

       System.out.println("oldVal = "+ oldVal + " and newVal = "+ newVal);

       TableColumn sortColumn = null;

       SortType st = null ;

       sortColumn = table.getColumns().get( newVal.intValue() ) ;

       st =  table.getColumns().get( newVal.intValue() ).getSortType() ;

       table.getSortOrder().clear();

       if(sortColumn != null){

       table.getSortOrder().add(sortColumn);

       sortColumn.setSortType(SortType.ASCENDING);

       }

      }

  });

  table.getSortOrder().addListener(new ListChangeListener<TableColumn<Person, ?>>(){

  @Override

  public void onChanged( Change<? extends TableColumn<Person, ?>> paramChange) {

       while(paramChange.next()) {

             if (paramChange.wasAdded()) {

       System.out.println("paramChanged.wasAdded() ");

       topBarCombo.valueProperty().bind( paramChange.getList().get(0).textProperty() );

       }

  }

  }

  });

}

  }

I was wondering if there is a better way to do this ? Demo code will be helpful. I had this posted in stackoverflow as well. Here is the link http://stackoverflow.com/questions/19740522/in-javafx-binding-combobox-items-and-table-column-sort

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2013
Added on Nov 3 2013
4 comments
1,344 views