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 the table column sort based on comboBox item slection with below code.
private void bindComboBoxAndTableColumnSort() {
ComboBox combo = topComboBarController.getSortCombo();
combo.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);
}
}
});
}
If someone can share some demo code, it will be helpful.