禁用时 JTree 的自定义节点图标
我有一个 JTree,其中根据树中的节点深度显示不同的图标,这是使用以下代码完成的:
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus){
super.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
int level = ((DefaultMutableTreeNode) value).getLevel());
JLabel label = (JLabel) this ;
if(level == 0) {
label.setIcon( new ImageIcon("icon1.gif") ) ;
}else if(level == 1) {
label.setIcon( new ImageIcon("icon2.png") ) ;
}
return this;
}
这按我的预期工作,除了如果禁用 JTree 组件,图标将恢复为(禁用)默认版本。 我是否需要创建图标的禁用版本?如果需要,如何使用此方法设置它们。我尝试添加:
if(!tree.isEnabled()) {
label.setIcon( new ImageIcon("icon1_disabled.gif") ) ;
}
...但我仍然保留禁用组件中的默认值。任何正确方向的提示将不胜感激。
I have a JTree in which I am displaying different icons according to a nodes depth in the tree, which I have done using the following code:
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus){
super.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
int level = ((DefaultMutableTreeNode) value).getLevel());
JLabel label = (JLabel) this ;
if(level == 0) {
label.setIcon( new ImageIcon("icon1.gif") ) ;
}else if(level == 1) {
label.setIcon( new ImageIcon("icon2.png") ) ;
}
return this;
}
This works as I would expect, except that if the JTree component is disabled, the icons revert to the (disabled) versions of the defaults.
Do I need to create disabled versions of my icons, and if, so, how are they set using this method. I tried adding:
if(!tree.isEnabled()) {
label.setIcon( new ImageIcon("icon1_disabled.gif") ) ;
}
... but I am still left with the defaults in a disabled component. Any hints in the right direction would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 label.setDisabledIcon(new ImageIcon("icon1_disabled.gif"));。我测试了它,似乎工作正常。
Use
label.setDisabledIcon(new ImageIcon("icon1_disabled.gif"));
. I tested it and seems to work fine.