无法在jTable中设置自定义背景颜色

时间:2014-09-05 06:16:18

标签: java swing jtable

我使用以下代码在jTable中设置备用背景行颜色:

table = new JTable(model);
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
{//alternate background color for rows
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,  boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (!isSelected)
           c.setBackground(row % 2 == 0 ? Color.white : Color.lightGray);
        return c;
    };
});

它有效。 但是我想使用自定义颜色说rgb(242,242,242)代替lightGray作为替代颜色,但是在做的时候:

final Color alternateColor = new Color (242,242,242);
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
{//alternate background color for rows
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,    boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (!isSelected)
           c.setBackground(row % 2 == 0 ? Color.white : alternateColor);
        return c;
    };
});

完全忽略备用颜色,并且所有行都具有白色背景。看起来此方法仅适用于属于Color枚举的颜色。怎么来的?

这是一个完整的工作示例,后面是结果屏幕:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Test extends JFrame
{
 private JTable             table               = null;
 private Object[]           columnTitle = { "Column1", "Column2", "Column3" };  
 private Object[][]         data                = null;
 private DefaultTableModel  model               = null;
 private JPanel             panel               = null;
 private JScrollPane        scroller            = null;

 public Test()
    {
        super();
        setLayout(new BorderLayout());
        panel = new JPanel();
        scroller=new JScrollPane();
        data = new Object[3][3];

        for (int i=0; i<3; i++)
            {
                for (int j=0; j<3; j++)
                    {
                        data [i][j]="Test";
                    }
            }

        model = new DefaultTableModel(data, columnTitle);
        table = new JTable(model);
        final Color alternateColor=new Color(242,242,242);
        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
        {//alternate background color for rows
                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                            boolean hasFocus, int row, int column)
                        {
                            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                            if ( !isSelected )
                                c.setBackground(row % 2 == 0 ? Color.white : alternateColor);
                            return c;
                        };
                });

        scroller.setViewportView(table);
        panel.add(scroller);
        this.add(panel, BorderLayout.CENTER);
    }

public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
                {
                    Test test = new Test();
                    test.setLocationRelativeTo(null);
                    test.pack();
                    test.setVisible(true);
                    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
        });
    }
}

enter image description here

这是使用Color.lightGray的屏幕: enter image description here

2 个答案:

答案 0 :(得分:2)

重写渲染器的方法仅在您拥有在所有列中使用默认渲染器的数据(即String)时才有效。如果表中的日期或数字数据需要不同的渲染器,该怎么办?

在这种情况下,请查看Table Row Rendering以获取适用于所有数据类型的解决方案。

或许你可以使用:

UIManager.put("Table.alternateRowColor", Color.YELLOW);

不知道这个属性是否适用于所有LAF。

答案 1 :(得分:1)

对我来说很好......

Test

import com.alee.laf.WebLookAndFeel;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class TestTable {

    public static void main(String[] args) {
        new TestTable();
    }

    public TestTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                WebLookAndFeel.install();

                DefaultTableModel model = new DefaultTableModel(new Object[]{"Test"}, 10);
                JTable table = new JTable(model);
                final Color alternateColor = new Color(255, 0, 255);
                table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {//alternate background color for rows
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                        if (!isSelected) {
                            c.setBackground(row % 2 == 0 ? Color.white : alternateColor);
                        }
                        return c;
                    }
                });
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

有些其他渲染可能是烤箱,你设置渲染器的表不是屏幕上的那个......许多其他因素表明你的问题runnable example会消除...