Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:

+Pie Number of slices to send: Send
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
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3751 times.
Similar Threads
adding a SelectItem object at particular index position in java.util.list
Populating JList in Parent window from Child window's save button
How to set a property's value thats part of a ArrayList using Javascript.
Add and Edit a row
Updating the value of Text box
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:24:18.