File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTree node as non editable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree node as non editable" Watch "JTree node as non editable" New topic
Author

JTree node as non editable

Suganthi Velliah
Greenhorn

Joined: Apr 30, 2007
Posts: 17
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Suganthi Velliah
Greenhorn

Joined: Apr 30, 2007
Posts: 17
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

Again, override getTreeCellEditorComponent. Get the result of super.getTreeCellEditorComponent, cast it to JTextField and set the columns.
Suganthi Velliah
Greenhorn

Joined: Apr 30, 2007
Posts: 17
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

Joined: Apr 30, 2007
Posts: 17
Please tell me where the flaw is?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree node as non editable