Exclude sort column in JTable

I have a simple Swing JTable and a TableRowSorter made by me. However, I would exclude the first column from sorting since I want it to display row numbers. I see nothing but

sorter.setSortable(0, false);

What makes a column not clickable, but still sortable when another column is clicked ... So quick question: how to save a column from a sorter using TableRowSorter?

Thank!

+3
source share
3 answers

So, when sorting JTable (ex below) in column A, you will get the following. However, you want the data to be sorted, but not line numbers, right?

|row| column A  |       |row| column A  |
+---+-----------+       +---+-----------+
| 1 | blah blah |  -->  | 1 | blah blah |
| 2 | something |       | 3 | more blah |
| 3 | more blah |       | 2 | something |

TableCellRenderer 0. , .

public class RowRenderer extends JLabel implements TableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object color,
        boolean isSelected, boolean hasFocus, int row, int column) {
        setText(Integer.toString(row));
        return this;
    }
}

:, (.. , , 100-200), row, .

+5

JTable , , , . TableModel, :

@Override public Object getValueAt(int rowIndex, int columnIndex) {
    if (columnIndex == 0) return rowIndex;
    else {
        // handle other columns
    }
}

, , .

+1

If the line number is not part of the data, it should not be stored in the model.

Instead, I would use a line header that displays the line number. What you saw in Excel. For this you can use the line number table .

0
source

Source: https://habr.com/ru/post/1790018/


All Articles