This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Design question involving GUI and classes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Design question involving GUI and classes." Watch "Design question involving GUI and classes." New topic

Design question involving GUI and classes.

Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
For example, you have a program where you first sign in, then you have a window (errm, frame ) open, in the frame there's a drop-down menu, each item on the menu opens a new frame, similar to the "tools" menu in your web-browser, as an example.

Now does the "sign-in" screen and each item on the menue get programmed in a separate class, where clicking on an item creates a new object of that class and has the code to display it in its constructor? Or is that not how you write a multiple-window program?
Jan van Mansum
Ranch Hand

Joined: Oct 19, 2007
Posts: 74
Add an ItemListener to the JComboBox and create the new JFrames in the event handler.

[ October 24, 2007: Message edited by: Jan van Mansum ]

[ October 24, 2007: Message edited by: Jan van Mansum ]
[ October 24, 2007: Message edited by: Jan van Mansum ]

SCJP 1.4, SCWCD 1.4
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's a common practice to have a Controller object that orchestrates the goings on between windows and the business/data side of an application. One common pattern is called "Model View Controller". Googling for that will find some technical and rather advanced discussions, so let's try to approach it more gently.

At a high level it might look like:

The Model here is some Plain Old Java Object that maybe checks the userid & password against a database. The Controller has the logic to decide what to do when authenticate succeeds or fails.

Showing a window calling the Controller skips over a lot of details - buttons and listeners and such. It sounds like you're getting comfortable with those things. Just make a button event handler that calls the Controller.

Other menu options should do much the same thing - just tell the Controller that something happened. I like a description of the controller "interpreting user gestures", messages like "The user hit this button" or at a slightly higher level "The user wants to close the application now"

Does that kind of thing make sense? Scroll down to the OO, UML, etc. forum for more detailed discussions of designs like this.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
Thanks a lot for the answers...

It's just when you tackle that first small project where design actually matters, it's scary how there are a million ways to do something right, and a billion ways to do it wrong, and you gotta pick one.
I agree. Here's the link:
subject: Design question involving GUI and classes.
It's not a secret anymore!