This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes OO Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "OO Question" Watch "OO Question" New topic

OO Question

Michael Salvini

Joined: May 25, 2010
Posts: 20
Hello All,

I am fairly new to java and object orientated programming in general. I have been developing in perl (without oop) for a couple of years now and I am branching out to java. I just completed Head First Java and the SCJA study guide. I want to spend some time developing small applications before I sit for the exam, because I really want to make sure I know Java (and can use it). Anyway, to my point. I have a rather hard time grasping the oo concepts and I wanted to run a class structure by you guys to see if I am doing this correctly (and with proper form).

I have a simple application (it doesn't do anything yet) with 3 classes. The Main class is RisSimulator with two other classes being Main Panel and MainMenuBar. RisSimulator builds the initial gui window, creating an instance of MainPanel (which creates an instance of MainMenuBar). Is this the proper way to construct this? I have included the code below. I am really just experimenting with different things right now.

I appreciate any and all input (even criticism).


Learning Java & Studying for the SCJA
Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

I don't see any point in your MainPanel class. It extends JPanel but doesn't modify its behaviour in any way. To put it in OO terms, you aren't using inheritance there. It could be replaced by a factory method which creates a JPanel and modifies it in a suitable way:

You'll notice that I did the same thing to your MainMenuBar class, but I didn't write the factory method (left as an exercise for the student).
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10747

Welcome to the Ranch.

Couple of pointers:
1) Swing is not thread safe. You should be using SwingUtilities#invokeLater to build and show your UI. A detailed discussion can be found here
2) You have subclassed JPanel. Ideally, you should be subclassing when you want to change the behavior. In your code you just changed the background and added a menu bar. Something like getContentPane().add(new JPanel()) would have been more apt. (More on menubar below)
3) JFrame provides a convenient method to set the menu bar, viz. setJMenuBar(). More info here You can always have a utility/helper class/method which constructs and returns the JMenuBar. Again you need not subclass JMenuBar

We have a dedicated UI forum for Swing/AWT/SWT. I will move this thread over there for you.

Damn! Paul beat me to it while I was typing!

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Michael Salvini

Joined: May 25, 2010
Posts: 20

I appreciate the help and pointers. I have rewritten the main class as you suggested. I guess now I am wondering, when do I break up this class? Let's say I start adding many functions to the menu bar and each one displays a different panel, maybe a form, something like that. Would I keep writing all of these helper methods in one class?


Thanks for the tips and the reading. I am reading through those articles now. Sorry about posting in the wrong forum, I wasn't sure which one to post in. I ultimately decided the Beginner one because I was more focused on understanding OO than the swing piece and now I think I am getting a better understanding of both.

Michael Salvini

Joined: May 25, 2010
Posts: 20

I have been reading through the links you sent me and I have a couple of questions. Below is the code I have created for reference.

1) Should all calls to gui/swing functions use the SwingUtilities.invokeLater() method? Things like for instance my status bar update method? (setStatusBarMessage)

2) In regards to the SwingWorker class, is this something you would use to when creating a new JPanel or form so you don't call set Methods on that form until it is complete, or am I missing the point with this?

I appreciate you time and help.


I agree. Here's the link:
subject: OO Question
It's not a secret anymore!