• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

ActionEvents/Swing Problem.

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Elam Daly
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic