• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to programmatically clik on a tree

 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying
MouseEvent ev = new MouseEvent(jTree,
MouseEvent.MOUSE_CLICKED,
System.currentTimeMillis(),
InputEvent.BUTTON1_MASK,
108,
30,
2,
false);
jTree.dispatchEvent(ev);
But it doesn't work. Please tell me what I am doing wrong.
 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to double click because I want to edit the node. I know that for trees it is triple click, bu my isCellEditable method accepts double click. I just tried jTree.startEditingAtPath(path) but that didn't work either. I am at a loss.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, the example program works perfectly; however, my code, shown below, still doesn't work.
jAddButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
TreePath path = jTree.getSelectionPath();
Object node = path.getLastPathComponent();
jTree.setEditable(true);
jTree.startEditingAtPath(path);
}
});
After the example I tried
int row = jTree.getRowForPath(path);
jTree.startEditingAtPath(jTree.getPathForRow(row));
In the debugger, the value of row is correct.

I have break points in the methods of my editor (extends TreeCellEditor) but the program doesn't stop on any of these methods.

I am running Java 1.5.0.6 with Eclipse (it worked for the example).

I am at a loss.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I pasted your code snippet into the test app and it works okay.
Is it possible that the instance variable jTree does not point
to the JTree instance that you are making selections in?
 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It i9s the correct tree, it gives the correct row. I think the problemm is that I am using a custom editor (see code below).
private class TreeCellEditor extends DefaultTreeCellEditor {
private Object nodeObject;

public TreeCellEditor(JTree tree, DefaultTreeCellRenderer treeCellRenderer) {
super(tree, treeCellRenderer);
}

public Object getCellEditorValue() {
return this.nodeObject;
}

public Component getTreeCellEditorComponent(JTree tree, Object node,
boolean isSelected, boolean expanded, boolean leaf, int row) {
if (!(node instanceof DefaultMutableTreeNode)) {
displayError("The tree nodes must be DefaultMutableTreeNode");
return super.getTreeCellEditorComponent(tree, node, isSelected,
expanded, leaf, row);
}
DefaultMutableTreeNode thisNode = (DefaultMutableTreeNode) node;
this.nodeObject = ((DefaultMutableTreeNode) node).getUserObject();
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) thisNode
.getParent();
boolean isFirstChild = parent.getChildBefore(thisNode) == null;
SmartTree.this.nodeEditor.setFirstChild(isFirstChild);
SmartTree.this.nodeEditor.setUserObject(this.nodeObject);
return (JPanel) SmartTree.this.nodeEditor;
}

/*
* Cells are editable only for doubleclick on leafs.
*/
public boolean isCellEditable(EventObject event) {
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
if (mouseEvent.getClickCount() == 1) return false;
TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent
.getY());
if (path != null && path.getPathCount() > 1) {
Object node = path.getLastPathComponent();
if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
return treeNode.isLeaf();
}
}
}
return false;
}
protected boolean canEditImmediately(EventObject event) {
return super.canEditImmediately(event);
}
protected boolean inHitRegion(int x,
int y) {
return true;
}



}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!