gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:43:46 25 4
gpt4 key购买 nike

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

最佳答案

仅当您的所有列中都有使用默认渲染器的数据(即字符串)时,覆盖渲染器的方法才有效。如果表中有需要不同呈现器的日期或数字数据怎么办?

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

或者也许你可以使用:

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

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

关于java - 无法在 jTable 中设置自定义背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com