• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OO Question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Michael Salvini
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

reply
    Bookmark Topic Watch Topic
  • New Topic