Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree node as non editable

 
Suganthi Velliah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Does anyone have idea on how to make some specific nodes in JTree as non editable.

My tree structure is as follows:

Root 1
|
Parent 1
|
Child 1
......
Child n
.....
|
Parent n

Root 2
Root 3
......
Root n

where I want the nodes Root 1, Root2....Root n to be non editable.
But the Parent nodes and child nodes to be editable.
The tree has popup which has options for renaming these nodes.
So I have set as follows

tree.setEditable(true);

How to make nodes Root 1, Root2....Root n to be non editable?

 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a custom TreeCellEditor and implement its isCellEditable method:
Alternatively, you can return a different editor component:
This will return a JLabel for editing the root and its direct children instead of a JTextField.

Disclaimer: I haven't tested either solution.
 
Suganthi Velliah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
It reallly helped me a lot.

I have another doubt.

When I click on the Parent and child nodes I am able to edit the node with MyTextField. I want Parent nodes to be of legth 15 where as the child nodes to be of length 20. How can I achieve this?

With the following code, I am getting both Parent and Child nodes to be of size 20. But I want different sizes. Please help me out!!!





 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, override getTreeCellEditorComponent. Get the result of super.getTreeCellEditorComponent, cast it to JTextField and set the columns.
 
Suganthi Velliah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done as follows. But I am getting ClassCastException in the return statement.Even I tried with JTextField also. I am getting the same exception
In the NonRootEditor class I have written as follows:



 
Suganthi Velliah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me where the flaw is?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suganthi Velliah wrote:Please tell me where the flaw is?

Apparently, DefaultTreeCellRenderer does not directly return the text field but wraps it in a Container subclass. DefaultCellEditor does not have this problem, so you should be able to use that one instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic