我正在使用以下代码在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);
                }
        });
    }
}




这是带有Color.lightGray的屏幕:

最佳答案

对我来说很好...



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消失了...

09-12 22:43