• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implement DocumentListener for dynamically added tf

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have jPanel1 that contains jTextField1 and addButton. addButton adding dynamic tf to jPanel2. Finally by saveButton retrieving their values and saving to text file. Now I am adding DocumentListener to each jTextField using this code:

And it works well with jTextField1, but it doesn't work for dynamically added tf.
SubPanel class:

I would like to ask how can I implement DocumentListener for dynamically added tf.
Any help is really appreciated.
 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got a JTextField so you can do exactly what you did for the other JTextField:



 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a GUI question and has nothing to do with I/O and Streams. Please try to choose a relevant forum when posting.

I'll move this to Swing / AWT / SWT for you.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul I tried your code.
I would like to ask how can I make DocumentListener global, i.e. accepatble for all jTextFields.
For example in text()method I want to getText of JTextField:

How can I assign textField for all jTextFields?
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds a very strange requirement. Please explain why you would want a “global” document listener and what you want it to do.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of one way to create a reusable class with a DocumentListener.

The button on the form will only be enabled when text has been entered into all the text fields (and will be disabled if you remove text from any text field):

 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tai Yo wrote: I would like to ask how can I make DocumentListener global, i.e. accepatble for all jTextFields.
For example in text()method I want to getText of JTextField:


Do you want that method to get the text from one specific JTextField, or from the JTextField to which the DocumentListener was attached?
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my case DocumentListener for jTextField1 working well. But in case of dynamic tf it doesn't work, I cannot cast tf to DocumentListener:

What is wrong with this code?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you reposting the same code. We don't have any new information to work with.

You have been given a complete working example. Start with the working example and customize the logic for your requirement.
 
Tai Yo
Ranch Hand
Posts: 98
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for long delay. Due to other stuff this problem was waiting own solution for a time. Just now I could return to this problem and quickly found where I was mistaken. Solution was so simple. I just moved addDocumentListener to SubPanel, it solved my problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!