Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 21330
87
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: 21330
87
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: 21330
87
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.
 
Something must be done about this. Let's start by reading this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!