0
votes

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.

2

2 Answers

2
votes

You need a second Listener which listens to the change to the changeorder of your TableView. Notice the need of the while loop to listen to the paramChange. Replace the ... with your binding to your ComboBox

tableView.getSortOrder().addListener(new ListChangeListener<TableColumn<ColumnClass, ?>>() {
    @Override public void onChanged(Change<? extends TableColumn<ColumnClass, ?>> paramChange) {
        while(paramChange.next()) {
            if (paramChange.wasPermutated()) {
                final TableColumn<ColumnClass, ?> first = paramChange.getList().get(0);
                final String tableColumnName = first.getText();
                ...
            }
        }
    }
});

Edit

According to the request some other approach

final ComboBox<String> box = new ComboBox<>();
table.getSortOrder().get(0).textProperty().bindBidirectional(box.valueProperty());
0
votes

With below code, i am able to achieve what @thatslch suggested.

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

            @Override
            //public void onChanged( javafx.collections.ListChangeListener.Change<? extends TableColumn<Person, ?>> paramChange) {
            public void onChanged( Change<? extends TableColumn<Person, ?>> paramChange) {  
                // TODO Auto-generated method stub
                while(paramChange.next()) {
                    if (paramChange.wasAdded()) {
                        System.out.println("paramChanged.wasAdded() ");
                        ComboBox combo = topComboBarController.getSortCombo();
                        combo.valueProperty().bind( paramChange.getList().get(0).textProperty() );

                    } 
                }

            }