且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jTable单元格背景色

更新时间:2023-12-03 11:26:40

首先,变量名称不应以大写字母开头.您的某些变量正确,而其他变量则不正确.保持一致!

First of all variable names should NOT start with an upper case character. Some of your variables are correct, others are not. Be consistent!!!

我尝试使用渲染器为jTable的单元格着色,但是它们滞后于表格并使其看不到是没有用的.

I've tried to color a cell of a jTable using the renderers, but they are useless they lag the table and make it impossible to see.

仅仅因为您不了解该概念并不会使其失效.问题出在您的代码上,而不是渲染器的概念上.

Just because you don't understand the concept does not make it useless. The problem is with your code, not the concept of renderers.

您发布的代码没有任何意义.您无法设置单个单元格的颜色.颜色是在单元格为渲染器时确定的,这就是为什么需要使用渲染器的原因.

Your posted code makes no sense. You can't set the color of an individual cell. The color is determined when the cell is renderer, which is why you need to use a renderer.

它完全为桌子上色

it colors the table completely

是的,一旦您设置了渲染器的背景,以后所有单元格都将使用该颜色.您需要在渲染每个单元格之前将颜色重置为其默认值

Yes, once you set the background of the renderer all cells in the future will use that color. You need to reset the color to its default before rendering each cell

背景必须为红色,以防万一,它是一个数字并且大于24,

the background must be red just in case it's a number AND it's higher than 24,

然后做一个积极的检查,而忘记所有那些消极的检查.

Then do a positive check and forget about all those negative checks.

使用以上所有建议,您可能需要一个渲染器,例如:

Using all the above suggestions you might have a renderer something like:

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)
            setBackground( table.getSelectionBackground() );
        else
        {
            setBackground( table.getBackground() );

            try
            {
                int number = Integer.parseInt( value.toString() );

                if (number > 24)
                    setBackground( Color.RED );
            }
            catch(Exception e) {}
        }

        return this;
    }
}