JTable可点击列排序:排序对单元格内容进行排序,但不更新单元格格式? [英] JTable Clickable Column Sorting: Sorting sorts content of cells, but doesn't update cell formatting?

查看:333
本文介绍了JTable可点击列排序:排序对单元格内容进行排序,但不更新单元格格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可排序的 JTable 设置为使用 AbstractTableModel 的自定义扩展名。但是,这个表的某些行为是我的预期,我想知道如何解决这个问题。



我将JTable设置为可排序使用:

  thisJTable.setAutoCreateRowSorter(true); 

这允许我按预期单击列标题对表进行排序。



但是,我发现当我通过单击列标题对表进行排序时,我的行的格式(背景和前景色)也没有排序。



我根据它们包含的值设置了这些行的颜色编码。当我按列标题排序时,给定行NUMBER的格式保持不变(尽管之前在该行中的内容已移动)。



通过覆盖JTable的默认 prepareRenderer 调用来设置行的颜色:

  thisTable = new JTable(thisModel){

//设置自定义渲染 - 将行的背景颜色设置为正确的值
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));
返回c;
}
};

有没有更好/不同的方法来解决这个问题?



我应该使用不同的方法来进行渲染吗,这种方法会更新排序中JTable的渲染吗?



或者我想要考虑编写我自己的排序方法?



解决方案(谢谢mKorbel!)



<我想我会发布我的解决方案,因为我不得不玩这个,因为我不确定新索引是否也会传递给prepareRenderer。

  thisTable = new JTable(thisModel){

//设置自定义渲染 - 设置背景颜色行的更正值
public Component prepareRenderer(TableCellRenderer renderer,int row,int column){

int viewIdx = row;
int modelIdx = convertRowIndexToModel(viewIdx);
组件c = super.prepareRenderer(渲染器,行,列);
CustTableModel thisModel =(CustTableModel)getModel();
c.setBackground(thisModel.getRowBackgroundColor(modelIdx));
c.setForeground(thisModel.getRowForeColor(modelIdx));
返回c;
}
};


解决方案

你必须从视图到模型转换行索引

  int modelRow = convertRowIndexToModel(row); 


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;
    }
};

解决方案

you have to convert row index from View to the Model

int modelRow = convertRowIndexToModel(row);

这篇关于JTable可点击列排序:排序对单元格内容进行排序,但不更新单元格格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆