使用 Netbeans IDE 在 jTree Java 上获取字符串值

发布于 2024-11-16 11:39:27 字数 1322 浏览 2 评论 0原文

我正在做一项作业,为酒店创建多房间选择。 我的想法是使用 jTree,这样我就可以选择 jTree 的多个子级。当我们使用 jTree 时,我们可以使用 ctrl+click 选择多个选项。我首先在 Netbeans 中创建项目,然后从 Pallete 添加 jTree。之后,我使用 TreeModel 的自定义代码并添加如下元素:

public void generateRoom() {
    DefaultMutableTreeNode room = new DefaultMutableTreeNode("Room");
    DefaultMutableTreeNode common = new DefaultMutableTreeNode("Common");
    DefaultMutableTreeNode vip = new DefaultMutableTreeNode("VIP");
    DefaultMutableTreeNode vvip = new DefaultMutableTreeNode("VVIP");

    room.add(common);
    room.add(vip);
    room.add(vvip);
    DefaultTreeModel model = new DefaultTreeModel(room);
    jTree1.setModel(model);
}

在构造函数中调用该方法后,我在 jTree 上添加了 MouseClick 事件。问题是我找不到如何通过 ctrl+click 获取多个子项。

谢谢,希望有人帮助我。

我尝试像这样添加 MouseOnclick :

private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    if (evt.getClickCount() == 1) {
        System.out.println(jTree1.getSelectionModel().toString());
    }
}

是的,它在控制台上得到了以下结果: 如果我只单击一个孩子,其结果是: javax.swing.tree.DefaultTreeSelectionModel 15980197 [ [Room, Common]@1 ]

如果我使用 ctrl+click,它会显示: javax.swing.tree.DefaultTreeSelectionModel 15980197 [ [Room, Common]@1 [Room, VIP]@2 ]

现在的问题是如何获取像 Common 或 VIP 这样的字符串。 谢谢..

I`m having a homework to create multi room selection for hotel.
My idea is by using jTree so I can select more more than one child of jTree. When we use jTree we can select more than 1 option by using ctrl+click. I started by creating project in Netbeans then added jTree from pallete. After that, I use custom code for TreeModel and add element like this :

public void generateRoom() {
    DefaultMutableTreeNode room = new DefaultMutableTreeNode("Room");
    DefaultMutableTreeNode common = new DefaultMutableTreeNode("Common");
    DefaultMutableTreeNode vip = new DefaultMutableTreeNode("VIP");
    DefaultMutableTreeNode vvip = new DefaultMutableTreeNode("VVIP");

    room.add(common);
    room.add(vip);
    room.add(vvip);
    DefaultTreeModel model = new DefaultTreeModel(room);
    jTree1.setModel(model);
}

After called the method in construcor, I added event MouseClick on jTree. The problem is I couldn`t find how to get multiple child from ctrl+click.

Thank you, hope somebody help me out..

I tried to add MouseOnclick like this :

private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    if (evt.getClickCount() == 1) {
        System.out.println(jTree1.getSelectionModel().toString());
    }
}

Yes, it worked with following result on console :
If I click only one child its result was :
javax.swing.tree.DefaultTreeSelectionModel 15980197 [ [Room, Common]@1 ]

If I use ctrl+click, it showed :
javax.swing.tree.DefaultTreeSelectionModel 15980197 [ [Room, Common]@1 [Room, VIP]@2 ]

The problem now is how do I get the string like Common or VIP..
Thank you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小鸟爱天空丶 2024-11-23 11:39:27

也许使用

public TreePath[] getSelectionPaths()

public int[] getSelectionRows()

Maybe use

public TreePath[] getSelectionPaths()

or

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