aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ActionEvents/Swing Problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ActionEvents/Swing Problem." Watch "ActionEvents/Swing Problem." New topic
Author

ActionEvents/Swing Problem.

Elam Daly
Greenhorn

Joined: Aug 09, 2003
Posts: 16
Howdy all,

I have a Swing mockup for a program in which I have created all relevant sections of the application as separate objects. So I have a SearchPanel object, in which the client types in a number and hits the search button, or the update button to update the database. Another object is the PatientData object, which merely displays all relevant patient data, etc. Each of the objects extends a JPanel, and in another object I compose them altogether in a JFrame. The main impetus for doing this was because I'm using JDeveloper's GUI building tools, and it's nice to be able to focus on each area of the appication in solitude.

I also have a dataStructure object in which I store all the data that will be updated in the database due to the client changing the data displayed in the Swing app.

Now,the problem I'm running into is how to register the dataStructure object with the relevant swing objects as receiving their event objects. The dataStructure object doesn't know anything about the Swing objects, because I create it in the main() method of the runtime object. So how am I to register the dataStructure with say a JButton in the swing app for instance? I could write a method is the object that contains the JButton, which accepts an instance of the dataStructure object and can therefore register said instance, but how do I call that method from the dataStructure object, considering the dataStructure object doesn't know that the swing app exists?

I realize this is a bit rambling, and I can be more concise if need be, as in code and class structure, so any help would be apprecitated.

Cheers.
[ July 27, 2005: Message edited by: Elam Daly ]
Scott Delap
author
Ranch Hand

Joined: Apr 05, 2005
Posts: 73
Elam,

Sounds like you have a view/model pattern design. Assuming this is the case the view needs to "observe" the model. So you will have to create some sort of listener interface that model (your data structure) is aware of. Then you'll need to add the view (your JPanels) which implement this interface to the model as listeners. Now your model can notify the view of events without having to be aware of Swing specificially. As far as where to do this registration process, I'd create some sort of external builder class that creates the model, the views, and then wires them together. For more pattern information I'd recommend you checkout some of Martin Fowler's wiki posts.

http://www.martinfowler.com/eaaDev/ModelViewPresenter.html

The next chapter of my book Desktop Java Live (to be released next week hopefully) also has a 50+ page chapter on wiring models and views together.

Scott Delap
ClientJava.com
Desktop Java Live
[ July 27, 2005: Message edited by: Gregg Bolinger ]
Elam Daly
Greenhorn

Joined: Aug 09, 2003
Posts: 16
Hi Scott,

Thanks for the reply.

I am aware of the MVC model, but am not familiar with implementing it in Swing, so thanks for the link.

Couldn't I also use a Singleton model, so that I have a static method that simply returns the instance of the dataStructure class in order to register it with the various relevant Swing objects?

Elam
[ July 27, 2005: Message edited by: Elam Daly ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by Elam Daly:
Hi Scott,

Thanks for the reply.

I am aware of the MVC model, but am not familiar with implementing it in Swing, so thanks for the link.

Couldn't I also use a Singleton model, so that I have a static method that simply returns the instance of the dataStructure class in order to register it with the various relevant Swing objects?

Elam

[ July 27, 2005: Message edited by: Elam Daly ]


Yes, you can do that. As long as you are sure that different views don't need their own data structure. With s singleton you would only ever have 1 copy in memory. So no matter what, everything is sharing the same data. Just a word of caution.


GenRocket - Experts at Building Test Data
Elam Daly
Greenhorn

Joined: Aug 09, 2003
Posts: 16
Okay last question,

The dataStructure now receives the event properly, but I still to need read values from various other objects in the gui, ie text fields and jlists. Is my best option to make static methods on the objects in question to send the dataStructure their current values?

Thanks again for the help.

Elam
Elam Daly
Greenhorn

Joined: Aug 09, 2003
Posts: 16
Okay, that apparently isn't going to work because of accessing a non-static variable from a static method.

So I guess then I have to register all data components with the dataStructure object, ie all JLists, JTextfields, and JTextareas, so that if their values change, the dataStructure object is aware and will update it's own variables to reflect the changes? The buttons are only going to be used as a 'trigger', to send/query the data to the database?

And in my dataStructure object, is it best to simply use a long if/then block to figure out which events are what?
[ July 29, 2005: Message edited by: Elam Daly ]
 
Don't get me started about those stupid light bulbs.
 
subject: ActionEvents/Swing Problem.