• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Dynamically adding components and accessing them afterwards.

 
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I'm trying to create simple swing application by using MVC pattern. I had it all figured out until stumbled upon problem where I had to add another panels dynamically on runtime.

I've created frame where I applied BoxLayout aligned to Y-axis. In that layout I have two different panels. First one of them I use to collect data from 2 JTextfields and second one to display something else in JTable.



Now let's say that until certain condition is met every time when user submits (click's the button) I add dynamically another JPanel identical to the first one except the button.



So far I have defined components private and used getter/setters in Controller class and it makes everything even more confusing.

What I can't figure out is how I dynamically add another JPanels and still be able to access those JTextfield values.

Thank you.
 
Sheriff
Posts: 28321
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
It looks like you already know how to add the new component. To access a JTextField inside that component, you need a reference to it. So when you create the JTextField, assign it to a variable. Then you can use that variable to access the JTextField.

(This has nothing really to do with Swing. It's a basic principle of any object-oriented language: if you want to work with an object, you need a reference to it.)
 
Kristjan Toots
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response Paul.
I am able to access JTextFields when I am dealing with single component. I'd do something like this. .



But when I have to add more components dynamically it gets tricky for me. I'll try to make an simple analog example. I'll use code found in stackoverflow.

The analog of the problem is that when I have dynamically added 5 JLabels saying "Bla". How would I approach when I need to say to the third JLabel object .setText("Changing third JLabel text");


 
Paul Clapham
Sheriff
Posts: 28321
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
From the programming point of view, you just keep all of those references in a List or something like that. However you really ought to have a design first.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add them to an ArrayList or similar
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic