• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implement DocumentListener for dynamically added tf

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got a JTextField so you can do exactly what you did for the other JTextField:



 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic