JTable 标题中的复选框与 Nimbus L&F 的呈现方式不同
我在 JTable 标题中有复选框,我使用 Nimbus L&F 并自定义标题背景,但复选框的背景采用默认的灰色背景,而不是像其他列那样自定义背景。
以下是 CheckboxRenderer 类
public class CheckBoxHeader extends JCheckBox implements TableCellRenderer{
public CheckBoxHeader(ItemListener itemListener) {
addItemListener(itemListener);
setHorizontalAlignment(SwingConstants.CENTER);
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JTableHeader header = table.getTableHeader();
Color bg = header.getBackground();
setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
return this;
}
,在 Table
TableColumn tc = getColumnModel().getColumn(0);
tc.setCellEditor(getDefaultEditor(Boolean.class));
tc.setCellRenderer(getDefaultRenderer(Boolean.class));
((JComponent)getDefaultRenderer(Boolean.class)).setOpaque(true);
tc.setHeaderRenderer(new CheckBoxHeader());
AFAIK 中,要正确渲染应使用 DefaultTableCellRenderer 渲染的标头,但我没有找到它的方法。有什么建议吗?
I have Checkbox in JTable Header, I am using Nimbus L&F and customize the header background but the background of the checkbox takes the default grey background not the customize one as others column do.
Following is the CheckboxRenderer class
public class CheckBoxHeader extends JCheckBox implements TableCellRenderer{
public CheckBoxHeader(ItemListener itemListener) {
addItemListener(itemListener);
setHorizontalAlignment(SwingConstants.CENTER);
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JTableHeader header = table.getTableHeader();
Color bg = header.getBackground();
setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
return this;
}
And in Table
TableColumn tc = getColumnModel().getColumn(0);
tc.setCellEditor(getDefaultEditor(Boolean.class));
tc.setCellRenderer(getDefaultRenderer(Boolean.class));
((JComponent)getDefaultRenderer(Boolean.class)).setOpaque(true);
tc.setHeaderRenderer(new CheckBoxHeader());
AFAIK to get the proper rendering of the header it should render with DefaultTableCellRenderer but i am not getting the way for it. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定它是否适用于所有 LnF(仅测试 Windows 7、Java 1.7.0_03):
I'm not so sure if it work on all LnF(testing only Windows 7, Java 1.7.0_03):