我使用下面的代码来设置备用背景色排在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说(242242242)代替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 enum的颜色。怎么会?
这是一个完整的工作示例接着将得到的画面:
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屏幕:
不,对我来说工作得很好。考虑提供一个[可运行的示例](https://stackoverflow.com/help/mcve),它可以证明你的问题。这将导致更少的混淆和更好的反应 – MadProgrammer 2014-09-05 06:20:02
如果它适合你,它可能与我正在使用的LAF是Weblaf有关。我没有看到任何其他可能的原因... – Zek101 2014-09-05 06:26:06
但第一个例子适用于weblaf? – MadProgrammer 2014-09-05 06:28:32