• 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

Populating textbox with the selected item from the pop-up window

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am new to Java, and currently developing the GUI application. In one of my forms I have a search button next to the textbox, which when clicked, brings up the pop up window with the list of items, and the select button. After selecting item from the list, and clicking the select button, the window should close and the textbox from the calling form should be populated. I am struggling a bit to populate a textbox with the selected item as these are two separate forms.

I would appreciate any kind of advice or a link to the solution for this type of problem.

Thanks in advance
Michael
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you point out, communicating between separate forms (or views) is challenging. A design approach to handle this challenge is known as MVC for Model-View-Controller. The Model is given full control of the program's data but does not directly alter user interfaces, or View. Views are constructed to accept input from the user but do not directly change the program's data, and the Controller contains the logic that displays the user interfaces with program data retrieved from the Model, interprets the user's actions in the View, and then passes those actions to the Model to cause the program's data to change accordingly.

Using the MVC concept to address your design challenge, you could use a Controller to show View A, your search button with text box. When the Controller detects that View A's search button is clicked, the Controller would display View B with your list of items. The Controller will record the item selected in View B, close View B, and populate the text box in View A with the selected item.

Piece of cake!
 
Michael Monchu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response Greg

I'm not sure if I am right, but I am thinking of having the main class (Controller) which has the inner classes (Views)? Is this how you were thinking of it, or am I wrong. I will appreciate if you can elaborate.

Thanks
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not exactly. Here's an MVC tutorial: MVC

I'm also a relative beginner at Java GUI programming, but I happened on the MVC design relatively early in my self study when I was trying to do the kinds of things you're asking about, and I have used it since. The concept is beyond the typical "do it all in one class" tutorial that you'll find. Those tutorials are designed to quickly explain how to code a small number of GUI elements, but when you start putting a lot of those elements together in several Views and want them to share information, you'll need to do some kind of functional separation with a Controller of something coordinating the whole thing - at least that's the construct that has worked well for me. There are probably other ways to accomplish the same thing. This construct also makes your code more maintainable and easier to understand, troubleshoot, add to, etc.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, y'all.

Champions, when using the Java Swing framework, it is more natural to use the Model-Delegate pattern, which is a small variation of the MVC architectural pattern. For a better understanding of the differences between these patterns, please take a look at this paper.

Now Mike, what you can do is pretty simple. You can have the main window with a method that updates the content of the JTextField, given a String. Something like this:



Champion, the secret is to pass the window object to the action listeners and put the methods that update/get data from the objects the compose the window in the classes that represent the windows. Action listeners can be seen as Controllers, but they have a more intimate relationship with the view, and this is the essence of the Model-Delegate pattern.

Please take a look at this code and understand it. If you have more questions, please let us know!
 
Michael Monchu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys

I will try it again and will let you know if I experience any problems. Your assistance is greatly appreciated.

Regards
Michael
 
Michael Monchu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys

I am pleased to let you know that I have modified my application according to the example you provided, and it's working fine.

Thanks a lot for your help.
Michael
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Mike.

Alright, good news! But the important thing here is to remember that, in the Java Swing world, an ActionListener is a Controller, that is, it knows both the UI and the business components. In a business application, you could pass the business component to the UI when you instantiate it via constructor, and pass this business component to the ActionListener via constructor so the business methods could be consumed there. Something like this:

 
Michael Monchu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for Roberto for sharing the knowledge. This has helped in taking care of some problems that I would have experienced.

Regards
Michael
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic