• 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

Creating JLabel and JTextfields at runtime

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my application, a db query is executed, gets the ids from db, & stores it in a list.
Then those many numbers of JLabel and JTextfields is created. Here is what i tried :


The list contains 2 numbers, 1 & 2. So the code creates 2 jlabels 1 & 2 with their respective textfields. So far, its good. But, how will i get their values after SUBMIT (JButton) is pressed? (JTextfields and Jlabels are declared inside the method, of whose code i have posted. )>
 
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
values can be retrieved by getText() method, isn't it?
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understood your problem well, you're asking how to access specific component (in this case JTextField) if you added it dynamically to your panel. Am I right?

If that is the case, once you handle the appropriate event (click on a button) you will need to iterate over all components placed inside your container and find the one that matches the name you are looking for. Once you access it, you can get its content with getText()
 
Marshal
Posts: 28193
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
When you create one of those components and add it to your GUI, keep a reference to it.

That doesn't mean you have to create a new variable to store the reference in. But you can certainly add it to a list of references, or something like that.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your replies. I have created a list of JTextFields and will get the values of JTextFields by iterating through the list.

Its something like this :

>
 
Message for you sir! I think it is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic