JTable Clickable Column Sorting: Sorting sorts content of cells, but doesn't update cell formatting?

泄露秘密 提交于 2019-11-27 15:48:55

问题


I have a sortable JTable set up to use a custom extension of the AbstractTableModel. However, some behavior of this table is what I expected, and I would love some advice on how to figure this out.

I have the JTable set up to be sortable using:

thisJTable.setAutoCreateRowSorter(true);

This allows me to sort the table by clicking on the column headers as expected.

However, I find that when I sort the table by clicking on the column headers, the formatting (background and foreground color) of my rows are not sorted as well.

I had set up those rows to be color-coded based on the values they contain. When I sort by column header the formatting at a given row NUMBER stays the same (although the content that was previously in that row moved).

The color of the row is set by overriding the default prepareRenderer call for the JTable:

thisTable = new JTable(thisModel){

    //Set up custom rendering - Sets background color of row to correct value
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        CustTableModel thisModel = (CustTableModel) getModel();
        c.setBackground(thisModel.getRowBackgroundColor(row));
        c.setForeground(thisModel.getRowForeColor(row));
        return c;
    }
};

Is there a better/different way to approach this?

Should I be using a different method to do my rendering, a method which would update the rendering of the JTable on a sort?

Or do I want to look into writing my own sorting method?

Solution (Thanks mKorbel!)

I thought I would post my solution, since I had to play with this a bit since I wasn't sure if the new index would be passed to the prepareRenderer as well.

 thisTable = new JTable(thisModel){

    //Set up custom rendering - Sets background color of row to correct value
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {

        int viewIdx = row;
        int modelIdx = convertRowIndexToModel(viewIdx);
        Component c = super.prepareRenderer(renderer, row, column);
        CustTableModel thisModel = (CustTableModel) getModel();
        c.setBackground(thisModel.getRowBackgroundColor(modelIdx));
        c.setForeground(thisModel.getRowForeColor(modelIdx));
        return c;
    }
};

回答1:


you have to convert row index from View to the Model

int modelRow = convertRowIndexToModel(row);



回答2:


You can enable table sorting by clicking on header using this table.setAutoCreateRowSorter(true);

for more information visit this site http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know



来源:https://stackoverflow.com/questions/10321038/jtable-clickable-column-sorting-sorting-sorts-content-of-cells-but-doesnt-upd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!