• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

TreeCellEditor on specific nodes and double click?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic