You’re going to need to create a custom cell editor. The reason for this is that the default ones only know how to edit cells that are simple data types. i.e. the object is the value (Strings, BigDecimals, Boolean etc). In your case a method in the object reveals the value.

 

First up you should create a custom tree cell editor (sub class DefaultTreeCellEditor), set a cell to use that editor and override _all_ the methods with debug messages to see exactly what methods are being called when.

 

Some hints and tips from some old code…

 

  • Looks like if “stopCellEditing” returns false then it won’t allow the user to move away from that cell with the changed value (veto the change).
  • Drill down into the code of the API – BasicTreeUI class has a method “completeEditing” which does the work of copying the new value back into the tree. If you’re using eclipse doing a “call hierarchy” is very helpful in this context.
  • In my code, to write the new value back I saved the original cell object being edited in the custom cell editor and then in “getCellEditorValue” did the work of copying the new value back into that cell object before returning the cell object.

 

In terms of the update to the persistence layer, you could put that in stop cell editing, though a better idea would be to add change listeners to the tree and then act based on those changes. Don’t forget to kick off a new thread to persist the change. If you’re lucky the JTree and DefaultTreeCellEditor classes will handle the firing of this event.

 

Looks like the article is correct – it’s probably the safest approach to do it that way – you’ll still need to create your custom cell editor though.

 


From: Enrico Goosen [mailto:[EMAIL PROTECTED]
Sent: 30 May 2006 11:25 AM
To: [email protected]
Subject: [CTJUG Forum] Editing a JTree DefaultMutableTreeNode

 

Hi All,

 

I want to make my JTree editable, allowing the user to rename a node.

The problem is that the change needs to be vetoed and the underlying user objects appropriate field needs to be updated.

 

I’ve googled for more info on how to do this.

The editing part seems easy…

I need to know how to get the newly entered value set in the user object.

One article recommends that I extend the DefaultCellEditor and wrap it in a DefaultTreeCellEditor?

Like this:

DefaultTreeCellEditor editor = new DefaultTreeCellEditor( 
              tree, defaultTreeCellRenderer, new MyCellEditor(textField));

 

If I go for the above approach, how do veto the change before updating the User Object and then persist that change to the database.

Are there events that I need to listen for?

 

Rgds,

 

Enrico Goosen

Software Developer

SAICOM TECHNOLOGY

TEL:       +2721 417 1375

FAX:     +2721 417 1361

CEL:     +2783 305 5676

EMAIL: [EMAIL PROTECTED]

 



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
-~----------~----~----~----~------~----~------~--~---


Reply via email to