调整 JTable 中的一个单元格而不是整行 [英] Adjust one cell in JTable instead of whole row

查看:44
本文介绍了调整 JTable 中的一个单元格而不是整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个小问题,正在努力解决.基本上发生的事情是我有一个 JTable,它由我从 API 调用中获得的数组填充.
我目前拥有的是,如果设备显示为在线,它将变为绿色,如果离线,则变为浅灰色.
问题是它影响了整个 ROW 而不仅仅是状态 CELL.我只希望状态单元格突出显示绿色.任何帮助将非常感激.

I have run into a slight issue and am struggling to resolve it. Basically what is happening is that I have a JTable which is being populated by an array that I get from an API call.
What I currently have is that if a device shows as online it will change GREEN, if offline, then a light-gray.
The problem is that it is affecting the entire ROW and not just the status CELL. I only want the status cell to highlight green. Any help would be really appreciated.

custTable = new javax.swing.JTable(model){

@Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
    int columnIndex) {
    JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);

    if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Online"))
    {
        component.setBackground(Color.GREEN);
    }
    else if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Offline"))
    {
        component.setBackground(Color.lightGray);
    }

    return component;
}

推荐答案

不要覆盖 prepareRenderer() 方法.通常,当您希望渲染对输入行有效时,您只会覆盖此方法.这种方法很有用,因为呈现代码在一个地方,您不必为表中的每一列创建自定义呈现器.

Don't override the prepareRenderer() method. Generally you would only override this method when you want the rendering to be effective for the enter row. This approach is useful because the rendering code is in one place and you don't have to create custom renderers for every column in the table.

但是,对于特定列中单元格的特定呈现,您应该为该列创建自定义呈现器.

However, for specific rendering of a cell in a specific column you should create a custom renderer for the column.

阅读 Swing 教程中关于使用自定义渲染器的部分 了解更多信息和示例.

Read the section from the Swing tutorial on Using Custom Renderers for more information and examples.

教程示例实现了 TableCellRenderer 接口.扩展默认渲染器可能更容易:

The tutorial example implement the TableCellRenderer interface. It may be easier to extend the default renderer:

class ColorRenderer extends DefaultTableCellRenderer
{
    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if (!isSelected)
        {
            int viewColumn = convertColumnIndexToView(1)
            String value = getModel().getValueAt(rowIndex, viewColumn).toString();

            if ("Online".equalsIgnoreCase(value))
                setBackground( Color.GREEN );
            else
                setBackground( Color.lightgray );

            return this;
        }
    }
}

请注意,您应该转换列索引,以防用户对表中的列重新排序.

Note you should convert the column index in case the user has reorder the columns in the table.

然后要使用渲染,您可以使用以下方法将其添加到各个列中:

Then to use the render you can add it to individual columns using:

TableCellRenderer colorRenderer = new ColorRenderer();
table.getColumnModel().getColumn(1).setCellRenderer( colorRenderer );

这篇关于调整 JTable 中的一个单元格而不是整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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