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() );
}
}
}
});
}
}