自定义TableCellRenderer / TreeTableCellRenderer不呈现表格单元格 [英] Custom TableCellRenderer/TreeTableCellRenderer doesn't render Table cells
问题描述
我创建了 CustomCellRenderer
类,用于 JXTreeTable
和 JXTable
对象,因为我的项目中有很多这些对象。所以这个类实现 TreeCellRenderer
和 TableCellRenderer
接口:
I made this CustomCellRenderer
class intended to be used in JXTreeTable
and JXTable
objects since I have many of these in my project. So this class implements TreeCellRenderer
and TableCellRenderer
interfaces:
public class CustomCellRenderer extends JLabel
implements TreeCellRenderer, TableCellRenderer {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
setBackground(selected ? new Color(83,142,213) : Color.white);
setForeground(selected ? Color.white : Color.black);
//here is the icon setting code but it's irrelevant to my problem
setText(value != null ? value.toString() : "<null>");
return this;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(isSelected ? new Color(83,142,213) : Color.white);
setForeground(isSelected ? Color.white : Color.black);
setText(value != null ? value.toString() : "<null>");
return this;
}
}
这是我设置渲染器的代码:
And here is the code where I set the renderer:
jXTreeTableConsumos.setDefaultRenderer(Object.class, new CustomCellRenderer());
jXTreeTableConsumos.setTreeCellRenderer(new CustomCellRenderer());
我预计当选择一行时背景和前景会分别变成蓝色和白色。然而,它只发生在树表格单元格(第一列),而只有前景发生变化,而背景仍然是白色在同一个选定行中的其他单元格中:
I'm expecting background and foreground become blue and white respectively when a row is selected. However it only happens at Tree table cell (first column) while only foreground changes and background stills white in the other cells in the very same selected row:
有人可以告诉我为什么是细胞(那个不是树细胞)不改变它们的背景颜色?
Could anybody please tell me why cells (that are not tree cells) don't change their background color?
推荐答案
感谢大家的意见和建议。我在中找到了解决方案JComponent #setBackground(Color bg)
documentation:
Thanks everybody for your comments and suggestions. I found the solution in JComponent#setBackground(Color bg)
documentation:
集此组件的背景颜色。背景颜色是
,仅当组件不透明时才使用 ,并且仅使用
JComponent或ComponentUI实现的子类。
JComponent的直接子类必须覆盖paintComponent才能使用此属性。
Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.
因为我的 CustomCellRenderer
从 JLabel
延伸我唯一需要做的就是确保它不透明并且它的背景颜色会被涂上:
Since my CustomCellRenderer
extends from JLabel
the only thing I have to do is make sure it's opaque and its background color will be painted:
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setOpaque(true);//adding this line I solved my problem
setBackground(isSelected ? new Color(83,142,213) : Color.white);
setForeground(isSelected ? Color.white : Color.black);
setText(value != null ? value.toString() : "<null>");
return this;
}
这篇关于自定义TableCellRenderer / TreeTableCellRenderer不呈现表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!