• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeCellEditor on specific nodes and double click?  RSS feed

 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a JTree which displays the names of natural languages (dutch, english, french) grouped by their families (romanic, germanic). When a language is selected, a page with info on it is opened in another pane, which, among other things, allows the user to alter its name. The families have no such page, so I need their nodes to be editable. I use one type of node, LanguageTreeNode. It can display both types:



In the panel where I show the JTree, I call it languagesTree, and I make it editable by doing:



FamilyTreeCellEditor is implemented as follows:



To store the new value, I have this method in my model:



Where FamilyHelper.update() is a static function that updates the record in the database and config.getCon() returns connection parameters for the database, which FamilyHelper.update() needs.

I have a few questions. First of all, how can I set the Language nodes as non-editable? I have searched for a setIsEditable() or setEditable() method but can't find any.

Secondly, the editor is always invoked on single-click, which is annoying, because I want a single click to only select the node. Where can I specify that only a double click (or whatever is the standard on the used look-and-feel/platform) should invoke the editor?

Thanks for any help.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to make your inputField editable/non editable.
 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer, but I don't understand. If I return a non-editable inputField, the user will still see a JTextField. Is there a way to not invoking the tree cell editor at all for specific leafs?
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diederick de Vries wrote:I have a JTree which displays the names of natural languages (dutch, english, french) grouped by their families (romanic, germanic).

...

how can I set the Language nodes as non-editable?


There are a couple of ways to do this.

You could override the editor's isCellEditable() method to return sometimes true and sometimes false. You have to convert the EventObject to a node before you can examine it, which is a bit of work, but JTree.getPathForLocation() is your friend.

a different approach: The editor's getTreeCellEditorComponent() method need not return the same Component every time. You may, for example, return a JTextField for some nodes and a JLabel for others. You just have to be careful with getCellEditorValue(). Whether this is worth the trouble rather than just returning an uneditable JTextField (perhaps with its border removed) is something you'll have to decide.
 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, a System.out.println (e.toString()) revealed the hidden nature of the EventObject. So the rest was pretty straightforward:



Thanks a lot! However, I'm still having to problem that the editor is invoked on single click, instead of double click. And I can't think why this is default behaviour. Does someone have some pointers on that one?

Thanks
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at DefaultCellEditor, you see that it uses a property called clickCountToStart, which is checked against the actual clicks of the MouseEvent. For text fields the click count is 2, for combo boxes and check boxes it is 1.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!