JTree 节点渲染和字体更改
我在 JTree 中渲染节点时遇到问题。当节点的字体更改且节点的文本变宽时,节点的文本将被剪切,文本末尾将替换为点。 那么如何告诉 JTree 它应该扩大区域以渲染整个节点。
谢谢你的帮助
I have a problem with rendering nodes in JTree. When node's font is changed and node's text gets wider that way then node's text is cut and end of text replaced with dots.
How to tell the JTree then that it should widen area to render tho whole node.
Thank you for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用自定义渲染器并将其设置为组件(JLabel),如下所示
,或者仅设置如下文本
You can use custom renderer and set to the component (JLabel) something like this
or just set text like this
听起来字体更改的触发发生在树的脚下:在内部,ui 委托执行大量大小缓存,必须根据影响缓存大小的任何更改进行更新。这是在对树模型、相关扩展状态以及树本身的一些视觉变化进行更改时自动完成的。
所以基本的问题是:什么触发了字体的变化?如果是模型/节点的某些更改,则模型实现不正确,没有触发适当的 TreeModelEvent,明显的解决方案是修复该问题:-) 如果它是模型之外的内容,则解决方案取决于您的上下文的详细信息,没有什么普遍适用的。
Sounds like the trigger of the font change happens under the feet of the tree: internally, the ui delegate does lots of size caching which must be updated on any change which effects the cached sizes. That's done automatically on changes to the treeModel, to relevant expansion state and some visual changes to the tree itself.
So the basic question is: what triggers the change of the font? If it's some change of the model/nodes, the model implementation is incorrect in not firing an appropriate TreeModelEvent, the obvious solution would be to fix that :-) If it's something outside the model, the solution depends on the details of your context, nothing generally applicable.
大小缓存
JTree 使用 渲染器 渲染节点。 渲染器是相同的渲染器对于所有操作系统,因此不同的外观位于 组件UI。 JTree 默认使用 JLabel 来绘制节点,因此 JLabel 的大小将指导我们使用
...
剪切文本。让我们做一个简短的讨论:Swing 对于不同的操作系统有不同的 LookAndFeels,它们与 UI 类中的组件分离,例如 BasicLabelUI (这是问题的根源)。 BasicLabelUI 缓存大小标签以防止在未进行任何更改的情况下重新计算。所以 BasicLabelUI 没有清除这些旧大小值的缓存。 BasicLabelUI 清除如果他收到有关任何更改的通知,则进行缓存。
问题是,为什么 BasicLabelUI 没有收到有关更改的通知?好吧,如果您以编程方式修改/扩展/重命名树,您必须告诉 ComponentUI 删除该缓存!
你很幸运,你不需要编写太多代码,因为天才已经为你写了一些东西,TreeUI 类的创建者 Rob Davis 和 Scott Violet 编写了 startEditingAtPath 和 stopEditing 。
例子
Size caching
The JTree uses a renderer to render nodes. The renderer is the same renderer for all OSs, so the differnt lookings are inside ComponentUIs. JTree uses by default a JLabel to paint nodes, so its the JLabel's size wo guides us to cut the text using
...
.Lets make a short excourse: Swing has differen LookAndFeels for different Operation Systems, they are detached from the components in UI-Classes like the BasicLabelUI (and this is the source of your problem). BasicLabelUI caches the size of the label to prevent recalculation if no changes has made. So BasicLabelUI did not clear the cache of theese old size values. BasicLabelUI do clear the cache if he gets informed about any changes.
The question is, why does the BasicLabelUI did not get informed about changes? Well, if you modify/expand/rename the Tree programatically you must tell the ComponentUI to drop that cache!
You lucky, you do not need to write much code because a genius already wrote something for you, the creators of the TreeUI-class Rob Davis and Scott Violet wrote startEditingAtPath and stopEditing.
Example
调用您的
TreeModel
的reload()
Call your
TreeModel
'sreload()