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 = 开发者_如何学Pythonnew 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..
Maybe use
public TreePath[] getSelectionPaths()
or
public int[] getSelectionRows()
精彩评论