This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Does anyone have idea on how to make some specific nodes in JTree as non editable.
My tree structure is as follows:
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
How to make nodes Root 1, Root2....Root n to be non editable?
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.
Again, override getTreeCellEditorComponent. Get the result of super.getTreeCellEditorComponent, cast it to JTextField and set the columns.
Joined: Apr 30, 2007
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 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.