• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Customising the editor in JTree

 
duncang groenewald
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply extend DefaultCellEditor, use the JTextField constructor, and override getCellEditorValue():
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
duncang groenewald
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic