File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Customising the editor in JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Customising the editor in JTree" Watch "Customising the editor in JTree" New topic
Author

Customising the editor in JTree

duncang groenewald
Greenhorn

Joined: Feb 24, 2009
Posts: 2
Hi, I want to modify the JTree cell editor so that it will strip off trailing characters from the cell label when a user edits the cell. Any idea how i can trap the event to do this ?

I though I should create my own TreeCellEditor using a customised JTextField but still I don't quite get how to grab the text and strip the trailing characters before passing control to the user so they can edit the text.

Just in case you are wondering why I want to strip trailing characters its because I include a count of child objects in the title by modifying the toString() function of the node object to append "(childcount)" to the end of the description or label. Its a pain when changing the description to have to delete this otherwise it gets added to the real label which is stored a the database. Hope this makes sense.

Thanks in advance

Duncan
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

Simply extend DefaultCellEditor, use the JTextField constructor, and override getCellEditorValue():


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
duncang groenewald wrote:Hi, I want to modify the JTree cell editor so that it will strip off trailing characters from the cell label when a user edits the cell. Any idea how i can trap the event to do this ?

I though I should create my own TreeCellEditor using a customised JTextField but still I don't quite get how to grab the text and strip the trailing characters before passing control to the user so they can edit the text.


I believe Mr. Prime's suggestion will strip characters after the user has edited the text. To do it before you can override your editor's getTreeCellRendererComponent() like this:

Just in case you are wondering why I want to strip trailing characters its because I include a count of child objects in the title by modifying the toString() function of the node object to append "(childcount)" to the end of the description or label. Its a pain when changing the description to have to delete this otherwise it gets added to the real label which is stored a the database. Hope this makes sense.


Well it "makes sense" in that I think I understand what you are doing, but I'm not sure how it makes sense to implement things in this way. Is there a particular reason you need to append the childcount to what toString() returns instead of making it accessible some other way? Keep in mind that your table model's getValueAt() method is not constrained to return a String but may, for example, return an Object with many getters.


bitguru blog
duncang groenewald
Greenhorn

Joined: Feb 24, 2009
Posts: 2
Actually I just found what i think is a better (simpler way) of achieving the same.

I create my own component as follows:


This seems to work just fine as the UserObject never includes the appended text so no stripping is required when the item gets saved in the database.

Thanks

 
 
subject: Customising the editor in JTree