File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "OO Question" Watch "OO Question" New topic
Author

OO Question

Michael Salvini
Greenhorn

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).



Thanks,
Mike


Learning Java & Studying for the SCJA
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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: 10175
    
    8

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 http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
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 http://download.oracle.com/javase/tutorial/uiswing/components/menu.html 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.
CarefullyChooseOneForum

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
Greenhorn

Joined: May 25, 2010
Posts: 20
Paul,

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?



Maneesh,

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.

Thanks,
Mike
Michael Salvini
Greenhorn

Joined: May 25, 2010
Posts: 20
Maneesh,

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.

Thanks,
Mike

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: OO Question
 
Similar Threads
Gridbag Layout
Right-Click on JMenuItem
JMenu Construction problem
JSplitPane not appearing
Creating one class from another