简体   繁体   English

如何更改第二行的背景颜色?

[英]How to change the background color of each second row?

I try to change the background color of each second row. 我尝试更改第二行的背景色。 The problem is that only first COLUMN is affected. 问题在于仅第一个COLUMN会受到影响。 Why? 为什么?

    table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
    {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
            return c;
        }
    });

Using the renderer approach you need to write a custom renderer for every data type in the table. 使用渲染器方法,您需要为表中的每种数据类型编写一个自定义渲染器。 So if you have String, Data, Integer, Boolean you would need to write 4 custom renderers. 因此,如果您具有String,Data,Integer,Boolean,则需要编写4个自定义渲染器。

See Table Row Rendering for an approach that will allow you to write the code once no matter haw many data types you have in the table. 请参阅表行渲染以获取一种方法,该方法将使您无论表中有多少数据类型,都可以立即编写代码。 This approach overrides the preparerrenderer(...) method of the JTable. 此方法重写JTable的preparerrenderer(...)方法。

As stated in Concepts: Editors and Renderers section of How to Use Tables tutorial, if you din not specify a renderer for a particular column then the table invokes the table model's getColumnClass method to get a default renderer for the column type. 如“ 如何使用表”教程的“ 概念:编辑器和渲染器”部分所述,如果您未为特定列指定渲染器,则该表将调用表模型的getColumnClass方法以获取该列类型的默认渲染器。

If you have overridden getColumnClass method then your approach might not work as expected. 如果您重写了getColumnClass方法,则您的方法可能无法按预期工作。 For instance: 例如:

DefaultTableModel model = new DefaultTableModel(new Object[]{"Column # 1", "Column # 2"}, 0) {
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        Class columnClass = Object.class;
        switch(columnIndex) {
            case 0: columnClass = String.class; break;
            case 1: columnClass = Boolean.class; break;
        }
        return columnClass;
    }
};

Then doing this: 然后这样做:

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {// your code here});

Won't work for the second column since the getColumnClass method will return Boolean.class and there's a default cell renderer for that class ( JCheckBox ). 由于getColumnClass方法将返回Boolean.class ,因此不适用于第二列,并且该类具有默认的单元格渲染器( JCheckBox )。

In your case I would suggest you override JTable.prepareRenderer() method instead to set rows background color independently of the renderer type ( JLabel , JCheckBox , or even custom renderers): 在您的情况下,我建议您重写JTable.prepareRenderer()方法,以独立于渲染器类型( JLabelJCheckBox甚至是自定义渲染器)设置行背景色:

JTable table = new JTable(model) {
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
        return c;
    }
};

Okay, scrap all of what I just wrote. 好吧,把我刚才写的所有东西都扔掉。

Attempt #2: 尝试2:

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
        return this;
    }
});

You need to be returning the current object, not the reference of the super() call. 您需要返回当前对象,而不是super()调用的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改ExpandableListView中每行的背景颜色? - Change background color of each row in ExpandableListView? 更改JTable中行的背景颜色 - Change the background color of a row in a JTable ListView中每行的颜色不同,我也想拖动该行,并且每行的背景颜色也要交换。 怎么做 - Different color for each row in a ListView, I want to drag the row and each row's background color exchange too. how to do it 如何更改给定行和索引处单元格的背景颜色? - how can I change the background color of a cell at a given row and index? 如何更改背景颜色动态创建的表行 - How to change background color dynamically created table row 选择后更改jtable中行的背景颜色 - change background color of row in jtable after selection 使用Apache POI更改行的背景颜色 - Change background color of row with Apache POI Android如何在每个listvIew列表项上添加图标并更改文本颜色,背景颜色 - Android How to add icon on each listvIew list item and change the text color,Background color 如何在Java中仅一秒钟更改文本框的背景颜色? - How can I change the background color of my textbox for just a second in Java? 如何使用彩色按钮作为用户选择从第二个活动更改我的 MainActivity 的背景颜色? - How can I change the background color of my MainActivity from a second activity using colored buttons as user choices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM