Computer07
asked on
Toggle JTree nodes
I want to unselect the a tree node , if we select/clicked on already a selected node. Is it possible to do this.
The code above is full code - you cvan compile and try to select/deselect
any node.
This is excerpt which shows the piece which has to do with selection:
Here bollean selected keeps track if we are clicking first or second time
If at the time of clicking selected is false - the node will be slected
and boolean will become true;
next click will deselect and toggle slected to false
class MyJTree extends JTree implements MouseListener {
HashMap m;
DefaultTreeSelectionModel sm = new DefaultTreeSelectionModel();
boolean selected;
public MyJTree(Vector v, HashMap m){
super(v);
this.addMouseListener(this);
this.m = m;
this.setSelectionModel(sm);
}
public void mouseClicked(MouseEvent me) {
if(selected){ sm.clearSelection();
selected = false;
}
else
selected = true;
TreePath tp = this.getPathForLocation(me.getX(), me.getY());
System.out.println("x:" + me.getX() + " y: " + me.getY());
// System.out.println(tp.toString());
DefaultMutableTreeNode node = null;
if(tp != null)
node = (DefaultMutableTreeNode)
tp.getLastPathComponent();
else return;
components.Book nodeInfo = null;
if(node.getUserObject() instanceof components.Book) {
nodeInfo = (components.Book)node.getUserObject();
System.out.println(nodeInfo.getPrice());
if(m.get(nodeInfo.getId()) == null)return;
System.out.println((Integer)m.get(nodeInfo.getId()));
if (me.getX()-35 > ((Integer)m.get(nodeInfo.getId())).intValue()){
System.out.println("button clicked");
}
}
}
public void mousePressed(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This coe provides example:
Open in new window