当节点扩展时,JTree 会扩大宽度

发布于 2024-11-06 07:54:58 字数 834 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-11-13 07:54:58

所描述的效果是 JPanel 的默认 FlowLayout 的特征。单击左侧面板时您可以看到更改是这样的 示例

附录:如 示例GridLayout允许组件展开;它与 setVisibleRowCount()pack() 配合使用效果特别好。

The effect described is characteristic of the default FlowLayout of JPanel. 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 with setVisibleRowCount() followed by pack().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文