• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JTree node as non editable  RSS feed

 
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?

 
Sheriff
Posts: 21459
96
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
Posts: 21459
96
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
Posts: 21459
96
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
Boost this thread!