我已经对 Swingx 的 ComponentProvider 进行了子类化以提供 JButton,但在 JXTreeTable 的某些行上,我不想显示任何按钮。我想要的最终“结果”是有一个空单元格,就像在我尚未设置提供程序的列中显示空文本时得到的结果一样。
是否可以隐藏某些行上的渲染组件(取决于例如值)?在
format()
或configureState()
中对渲染组件设置setVisible(false)
不起作用。是否可以子类化 ComponentProvider 以提供不同类型的组件?如果是,那将如何运作?
我在哪里可以找到 ComponentProvider 提供的一些可能性的示例,以及哪个方法做什么的清晰解释(例如,我很难理解
configureState()
和format()
)?
编辑
是否可以阻止 JX(Tree) 中显示的 JButton?表格与单元格一样宽?
如果我创建另一个荧光笔,我可以使用另一个谓词(ROLLOVER 或其他)来更改光标吗?即使按钮被隐藏,光标也会变成手形(在链接上方)。
非常感谢!
最佳答案
有趣:-)
- 通常不起作用,因为 CellRendererPane 不尊重组件的可见属性 - 它总是标记它。但是:如果将实际提供程序包装到 WrappingProvider 中,然后将该包装器的组件设置为不可见,则可以在 SwingX 中工作。
一个片段,就像概念验证一样
table.getColumn(1).setCellRenderer(new DefaultTableRenderer(
new WrappingProvider(IconValues.NONE, new ButtonProvider(), false) {
@Override
protected void configureState(CellContext context) {
super.configureState(context);
rendererComponent.getComponent().setVisible(context.getRow() != 5);
}
}
));
另一方面,提供程序不是插入自定义上下文相关配置的地方。这应该在荧光笔中完成,如 f.i.在
AbstractHighlighter highlighter = new AbstractHighlighter(HighlightPredicate.EVEN) {
@Override
protected Component doHighlight(Component component,
ComponentAdapter adapter) {
((WrappingIconPanel) component).getComponent().setVisible(false);
return component;
}
@Override
protected boolean canHighlight(Component component,
ComponentAdapter adapter) {
return component instanceof WrappingIconPanel;
}
};
table.addHighlighter(highlighter);
这不能按预期工作(按钮始终隐藏),因为不是保证在提供程序中重置的属性之一。没有什么可以阻止自定义提供商扩展这些保证,例如
table.getColumn(1).setCellRenderer(new DefaultTableRenderer(
// custom wrappingProvider which guarantees the reset of visible
// property of the wrapped component
new WrappingProvider(IconValues.NONE, new ButtonProvider(), false) {
@Override
protected void configureState(CellContext context) {
super.configureState(context);
rendererComponent.getComponent().setVisible(true);
}
}
));
现在荧光笔可以根据上下文无畏地改变可见内容。一个轻微的视觉问题:WrappingIconPanel 总是为图标留下一些空间,即使没有 - 不太确定为什么会发生这种情况,或者删除该间距是否安全(在 SwingX 中)(wrappingProvider 最初是用于在JXTree,默认情况下没有安装,因为 ComponentOrientation 仍然存在问题)。
(问题中的 2)不受支持,ComponentProvider 旨在返回在每次调用时配置完全相同属性的同一个组件
(问题中的 3)咳...不,只有来源和示例(在演示和测试包中)
编辑(回答问题的已编辑部分)
不,对于当前的 WrappingIconpPanel:它确实使用了 Borderlayout - 众所周知:-) 不尊重最大尺寸。使用 BoxLayout 会出现一些我不完全记得的问题。尽管如此,这将是调整的地方,以便尊重按钮的最大值
嗯...不完全确定您是如何实现光标更改的。假设它在您的 ButtonProvider 中:实现 isRolloverEnabled 以根据它是否可见来返回 true/false Edit-in-Edit 不起作用。不知道为什么,可能是这是翻转检测和/或 WrappingProvider 处理中的错误
现在就到周末了:-)
关于java - Swingx组件提供者: hide component on certain rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311455/