• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating JLabel and JTextfields at runtime

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • 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. )>
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
values can be retrieved by getText() method, isn't it?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • 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()
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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 :

>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic