当节点扩展时,JTree 会扩大宽度
我在 JTree 方面遇到了一个真正的难题。
我已经实现了一个带有自写模型的 JTree(它是从 TreeModel 扩展的)。
fileSystemModel = new MyModel(new File(directory));
fileTree = new JTree(fileSystemModel);
fileTree.setEditable(true);
fileTree.setDragEnabled(true);
fileTree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent event) {
File file = (File) fileTree.getLastSelectedPathComponent();
}
});
所以我将它添加到 JPanel 中,并将该 JPanel 添加到 JFrame 中。
不,我遇到了这个问题:我启动框架,如果我展开单个节点,JTree 的宽度将放大到显示叶子所需的宽度。
我尝试设置这个:
fileTree.setMaximumSize(width, height);
但它失败了,我还尝试将 JTree 嵌入到具有最大大小的 JScrollPanel 中,这也失败了。
JTree 仍在根据需要调整其宽度。
所以我现在尝试了两天,但没有找到任何东西,所以如果你们中有人能帮助我,我将不胜感激。
谢谢!
I've got a real hard problem with a JTree.
I've implemented a JTree with a selfwritten Model (wich is extended from TreeModel).
fileSystemModel = new MyModel(new File(directory));
fileTree = new JTree(fileSystemModel);
fileTree.setEditable(true);
fileTree.setDragEnabled(true);
fileTree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent event) {
File file = (File) fileTree.getLastSelectedPathComponent();
}
});
So i added it to a JPanel and this JPanel to a JFrame.
No i got this Problem: I start the Frame, and if i expand a single Node, the width of JTree is going to enlarge to the width which is needed to show the leafs.
I tried to set this:
fileTree.setMaximumSize(width, height);
But it failed and i also tried to embedd the JTree into JScrollPanel with a Maximum-Size, and this has also failed.
The JTree is still adjusting its width how many it needs.
So i tried now for two days and doesn't find anything, so i would be apprecciate if someone of you could help me.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所描述的效果是
JPanel
的默认FlowLayout
的特征。单击左侧面板时您可以看到更改是这样的 示例。附录:如 示例,
GridLayout
允许组件展开;它与setVisibleRowCount()
和pack()
配合使用效果特别好。The effect described is characteristic of the default
FlowLayout
ofJPanel
. You can see the change when you click on the left panel is this example.Addendum: As seen in the example,
GridLayout
allows the component to expand; it works particularly well withsetVisibleRowCount()
followed bypack()
.