使用 Netbeans IDE 在 jTree Java 上获取字符串值
我正在做一项作业,为酒店创建多房间选择。 我的想法是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许使用
或
Maybe use
or