使用TableCellRenderer进行Java JTable排序 [英] Java JTable sort with TableCellRenderer

查看:138
本文介绍了使用TableCellRenderer进行Java JTable排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java很新,我遇到了 JTable 的问题,排序和 TableCellRenderer

I'm somewhat new to Java and I'm having problems with JTable with sort and TableCellRenderer.

我有一个包含15个列的表,其值在某些列上我使用 TableCellRenderer 来更改颜色前景为绿色或红色根据单元格上的值。

I have a table with 15 columns populated with values where on some columns I use the TableCellRenderer to change the color of foreground to green or red acording the values on the cell.

一切正常,直到我尝试按某些列排序(排序部分没问题)...
问题是格式化颜色不能反映排序操作所做的更改。在排序之前,颜色在桌子上的原始位置保持不变。有没有一种简单的方法可以解决这个问题?

Everything is working perfectly until I try to sort by some column (the sorting part is ok)... The problem is that the formating colors don't reflect the change made by the sort operation. The colors stay unchanged at their original position on the table before the sort. Is there a simple way to overcome this problem?

我搜索过,但是我找不到可以实现的解决方案。

I searched, but I did not find a solution I can implement.

TIA

JL

编辑:相关发布的源代码(未完成实现)

Related source code posted (not complete implementation)

private void buildTab(){
    myIUserInterface.removeBottomTab("just a test");
    myJPanel = myIUserInterface.getBottomTab("just a test");
    myJScollPane = new JScrollPane();
    myTable = new JTable();
    myTable.setAutoCreateRowSorter(true);
    myTableContent = myFillTableData();
    String[] tableHeaders = {
            "X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15"
        };
    myTable.setModel(new DefaultTableModel(myTableContent, tableHeaders) {

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    });
    MyChangeCellColor myCCC = new MyChangeCellColor();
    Enumeration<TableColumn> allColumns = myTable.getColumnModel().getColumns();
    while(allColumns.hasMoreElements()){
        TableColumn column = allColumns.nextElement();
        column.setCellRenderer(myCCC);
    }
    myJScollPane.setViewportView(myTable);

    GroupLayout myLayout = new GroupLayout(myJPanel);
    myJPanel.setLayout(myLayout);
    myLayout.setHorizontalGroup(
        myLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(myJScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 890, Short.MAX_VALUE)
    );
    myLayout.setVerticalGroup(
        myLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(myJScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 672, Short.MAX_VALUE)
    );
}


class MyChangeCellColor extends JLabel implements TableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        setHorizontalAlignment(CENTER);
        setOpaque(true);
        setBackground(Color.WHITE);
        if (column == 0){
            setForeground(Color.BLACK);
        }
        if (column == 1){
            setForeground(Color.BLACK);
        }
        if (column == 2){
            setForeground(Color.BLACK);
        }
        if (column == 3){
            setForeground(Color.BLACK);
        }
        if (column == 4){
            if ("LONG".equals(myTable.getModel().getValueAt(row, 4))) {
                setForeground(Color.GREEN.darker());
            }
            if ("SHORT".equals(myTable.getModel().getValueAt(row, 4))) {
                setForeground(Color.RED);
            }
        }
        if (column == 5){
            setForeground(Color.BLACK);
        }
        if (column == 6){
            setForeground(Color.BLACK);
        }
        if (column == 7){
            setForeground(Color.BLACK);
        }
        if (column == 8){
            setForeground(Color.BLACK);
        }
        if (column == 9){
            setForeground(Color.BLACK);
        }
        if (column == 10){
            setForeground(Color.BLACK);
        }
        if (column == 11){
            double d = Double.valueOf(myTable.getModel().getValueAt(row, 11).toString());
            if (d > 0) {
                setForeground(Color.GREEN.darker());
            }
            if (d == 0) {
                setForeground(Color.BLACK);
            }
            if (d < 0) {
                setForeground(Color.RED);
            }
        }
        if (column == 12){
            double d = Double.valueOf(myTable.getModel().getValueAt(row, 12).toString());
            if (d > 0) {
                setForeground(Color.GREEN.darker());
            }
            if (d == 0) {
                setForeground(Color.BLACK);
            }
            if (d < 0) {
                setForeground(Color.RED);
            }
        }
        if (column == 13){
            setForeground(Color.BLACK);
        }
        if (column == 14){
            setForeground(Color.BLACK);
        }
        setText(value.toString());
        return this;
    }
}

编辑2:解决方案喜欢并粘贴以供参考需要它!只需要在TableCellRenderer类上引用JTable返回的行上的模型行(不确定它是否正确解释,你可以看到下面的更改):

EDIT 2: Solution fond and pasted for reference if anyone needs it! Just need to reference the model rows on the rows returned by JTable on TableCellRenderer class (not sure if it's correctly explained anyway you can see the changes below):

class MyChangeCellColor extends JLabel implements TableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        setHorizontalAlignment(CENTER);
        setOpaque(true);
        setBackground(Color.WHITE);
        if (column == 0){
            setForeground(Color.BLACK);
        }
        if (column == 1){
            setForeground(Color.BLACK);
        }
        if (column == 2){
            setForeground(Color.BLACK);
        }
        if (column == 3){
            setForeground(Color.BLACK);
        }
        if (column == 4){
            if ("LONG".equals(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 4))) {
                setForeground(Color.GREEN.darker());
            }
            if ("SHORT".equals(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 4))) {
                setForeground(Color.RED);
            }
        }
        if (column == 5){
            setForeground(Color.BLACK);
        }
        if (column == 6){
            setForeground(Color.BLACK);
        }
        if (column == 7){
            setForeground(Color.BLACK);
        }
        if (column == 8){
            setForeground(Color.BLACK);
        }
        if (column == 9){
            setForeground(Color.BLACK);
        }
        if (column == 10){
            setForeground(Color.BLACK);
        }
        if (column == 11){
            double d = Double.valueOf(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 11).toString());
            if (d > 0) {
                setForeground(Color.GREEN.darker());
            }
            if (d == 0) {
                setForeground(Color.BLACK);
            }
            if (d < 0) {
                setForeground(Color.RED);
            }
        }
        if (column == 12){
            double d = Double.valueOf(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 12).toString());
            if (d > 0) {
                setForeground(Color.GREEN.darker());
            }
            if (d == 0) {
                setForeground(Color.BLACK);
            }
            if (d < 0) {
                setForeground(Color.RED);
            }
        }
        if (column == 13){
            setForeground(Color.BLACK);
        }
        if (column == 14){
            setForeground(Color.BLACK);
        }
        setText(value.toString());
        return this;
    }
}


推荐答案


问题是格式化颜色不能反映排序操作所做的更改。

The problem is that the formating colors don't reflect the change made by the sort operation.

你有渲染器中的逻辑问题。您可能正在使用视图行而不是模型行访问表模型中的数据。

You have a problem with the logic in your renderer. You are probably accessing data in the table model with the "view row" instead of the "model row".

因为您没有发布 SSCCE 演示问题我不能给你一个特定的解决方案只是一般的解决方案。查看以下JTable方法:

Since you didn't post your SSCCE demonstrating the problem I can't give you a specific solution only a general solution. Look at the following JTable methods:

convertRowIndexToModel(int viewRowIndex) 
convertRowIndexToView(int modelRowIndex)  

渲染器收到视图行号。

这篇关于使用TableCellRenderer进行Java JTable排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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