0

I got a problem when filtering my JTable. In fact, on my first row I have JComboBoxes in each column. And when I sort which the items of JCombo the first row is also being filtered and disappear.

Here is my Table model:

public static class MyModel extends AbstractTableModel {

    private static final long serialVersionUID = -768739845735375515L;
    private List<Object[]> data;
    private List<String> columnNames;

    public MyModel(List<String> columnNames, List<Object[]> data) {
        super();
        this.columnNames = columnNames;
        this.data = data;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.size();
    }

    @Override
    public String getColumnName(int column) {
        return columnNames.get(column);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data.get(rowIndex)[columnIndex];
    }

    @Override
    public boolean isCellEditable(int row, int col) { // Pour modifier uniquement la cellule Statut
        if (row == 0) {
            return true;
        } else {
            return false;
        }
    }

    public void setValueAt(Object value, int row, int col) {
        data.get(row)[col] = value;
        fireTableCellUpdated(row, col);
    }

    public void removeRow(int row) {
        data.remove(row);
    }
}

And the combo listener :

private void ComboListener(final JComboBox comboBox){   
    comboBox.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    try{
                    String selectedItem = comboBox.getSelectedItem().toString();
                    sorter.setRowFilter(RowFilter.regexFilter(selectedItem));
                    }catch(Exception ex){}
                }
            }            
    );
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
willyRG
  • 83
  • 1
  • 8

1 Answers1

0

I'm assuming you're talking about filtering, not sorting, based on your current code.

You could make use of the int.. indices argument when creating your filter:

int[] indices = new int[yourModel.getRowCount() -1];
for (int i = 0; i < indices.length; i++) {
  indices[i] = i+1;
}

sorter.setRowFilter(RowFilter.regexFilter(selectedItem, indices));

This will cause your filter to apply to all the rows except row 0.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254