使用表格的单元格标题颜色自定义 jtable cellrenderer [英] customizing jtable cellrenderer with table's cell header color
问题描述
这个问题与我的 previous post 非常相似.我需要自定义 JTable 的某些单元格,使其看起来像表格标题单元格.我正在使用 Nimbus 外观和感觉,我正在尝试检索 JTable 编辑器的颜色:
That's a question really similar to this previous post of mine. I need to customize some cell of a JTable, in a way that they would look like a table header cell. I'm using Nimbus look and feel and I'm trying to retrieve the color of JTable's editor:
public class HeaderCellRenderer extends JLabel implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
System.out.println("OK");
this.setOpaque(true);
setBackground(UIManager.getColor("TableHeader.background"));
return this;
}
}
我按照此 post 获取要提供给 getColor 方法的名称(TableHeader.背景").尽管我从现在开始做了什么,但返回的颜色与我的表格标题单元格的颜色不同.
I follow this post to get the name to be supplied to getColor method ("TableHeader.background"). Despite what I've done since now, the color returned isn't the same of my table's header cells.
你有什么想法吗?
我注意到它应该是渐变而不是颜色,但我找不到要使用的正确键.我查看了这个列表.
I noticed that instead of a color it should be a gradient but I can't find the right key to use. I looked this list.
推荐答案
典型 Look & 的默认表头的外观感觉由 的实例提供sun.swing.table.DefaultTableCellHeaderRenderer
.您可以通过以下方式获取副本:
The appearance of the default table header for a typical Look & Feel is provided by an instance of sun.swing.table.DefaultTableCellHeaderRenderer
. You can obtain a copy as follows:
class HeaderRenderer implements TableCellRenderer {
TableCellRenderer renderer;
public HeaderRenderer(JTable table) {
renderer = table.getTableHeader().getDefaultRenderer();
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
return renderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
}
}
并且您可以为给定列的 类型以通常的方式安装它令牌:
and you can install it in the usual way for a given column's type token:
table.setDefaultRenderer(SomeObject.class, new HeaderRenderer(table));
这篇关于使用表格的单元格标题颜色自定义 jtable cellrenderer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!