Swing - 根据单元格的值设置单元格的颜色 [英] Swing - Setting the color of a cell based on the value of a cell

查看:149
本文介绍了Swing - 根据单元格的值设置单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据单元格的值设置单元格的颜色。谷歌搜索了一下我发现我可以使用这样的东西:

I would like to set the color of a cell based on the value of the cell. Having googled around for a bit i found out that i can do it using something like this:

public class TableCellRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(
       JTable table, Object value, 
       boolean isSelected, boolean hasFocus, 
       int row, int col)  
    {
       // get the DefaultCellRenderer to give you the basic component
       Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
       // apply your rules
       if (value.toString().equals("Red"))
          c.setBackground(Color.RED);
       else  
          c.setBackground(Color.GRAY);
       return c;
    }
 }

我遇到的问题是代码我会喜欢修改已经为JTable的列设置了TableCellRendererer。代码中有一个函数如下所示:

The problem i have though is that the code i would like to modify is already setting the TableCellRendererer for the columns of the JTable. There is a function in the code that looks like this:

private void configureTableColumns() {      
        Enumeration columns = this.table.getColumnModel().getColumns();

        while (columns.hasMoreElements()) {
            TableColumn tableColumn = (TableColumn) columns.nextElement();

            this.setCellRenderer(tableColumn);
            TableCellRenderer renderer = tableColumn.getCellRenderer();

            if (renderer == null) {
                renderer = this.table.getDefaultRenderer(Object.class);
            }
            tableColumn.setCellRenderer(renderer);
            this.setCellEditor(tableColumn);
        }
    }

使用上面的代码,我还需要添加之前显示的TableCellRenderer类?我想要做的就是检查单元格的值是否为ABC并将背景设置为RED。

With the above code, do i still need to add the TableCellRenderer class shown previously? All i want to do is to check if the value of the cell is 'ABC' and set the background to RED.

我确实尝试将我的TableCellRenderer版本添加为我想要的代码中的内部类修改,但我得到一个错误,表格类型不匹配tableColumn.getCellRenderer()。

I did try adding my version of the TableCellRenderer as an inner class in the code i want to modify but i get an error that there is a type mismatch at tableColumn.getCellRenderer().

Type mismatch: cannot convert from TableCellRenderer to MyTableExample.TableCellRenderer

谢谢

推荐答案

覆盖 prepareRenderer 执行此操作的方法。

Override prepareRenderer method for doing that.

示例:

public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){  
Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex);  

if(getValueAt(rowIndex, columnIndex).toString().equals("Red")) {  
   componenet.setBackground(Color.RED);  
} else if(getValueAt(rowIndex, columnIndex).toString().equals("Green")) {
   componenet.setBackground(Color.GREEN);
}
return componenet;
} 

更新:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;


public class PrepareRendereEx {

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setSize(new Dimension(400, 100));

    Object data[][] = { {"java", "j2ee"}, 
                        {"java", "j2ee"}, 
                        {"java", "j2ee"}
                      };
    Object[] columnNames = {"Java", "J2EE"};

    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(model) {
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
                int columnIndex) {
            JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);  

            if(getValueAt(rowIndex, 0).toString().equalsIgnoreCase("java") && columnIndex == 0) {
                component.setBackground(Color.RED);
            } else if(getValueAt(rowIndex, 1).toString().equalsIgnoreCase("j2ee") && columnIndex == 1){
                component.setBackground(Color.GREEN);
            }

            return component;
        }
    };

    frame.add(new JScrollPane(table));
    frame.setVisible(true);
}
}

输出:

这篇关于Swing - 根据单元格的值设置单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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