• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restrict Editing the node in JTree on reaching its maximum length

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

I have a doubt in JTree.

I have a Tree structure as follows:

Parent1
|
Child 1
Child 2
Child 3

Where I get popup on right click of the node with options Add, Remove & Rename. So I can rename the node. The node name’s length should be <=16 characters. So while renaming the node, I want to stop the keystrokes if it reach 16 characters.

Do you have any idea to do this?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out DocumentListener or DocumentFilter API.
Essentially you need to provide a custom DocumentXXX to the JTextField which the user is using to edit the name.

To get a rough idea on how it should be done check this out http://faq.javaranch.com/java/NumericTextField
 
Suganthi Velliah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maneesh

I have solved the problem.

We already have cutomized JTextField for restricting the keystrokes once it reaches the maximum length.

I have done as follows:

MyTextField textField = new MyTextField(16);
TreeCellEditor editor = new DefaultCellEditor(textField);

tree.setEditable(true);
tree.setCellEditor(editor);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic