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.
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!
Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Joined: Oct 20, 2010
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.
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.
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!
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:
Joined: Oct 20, 2010
Thanks a lot for Roberto for sharing the knowledge. This has helped in taking care of some problems that I would have experienced.
subject: Populating textbox with the selected item from the pop-up window