File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Preferred/Best Practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Preferred/Best Practice" Watch "Preferred/Best Practice" New topic

Preferred/Best Practice

Bd Howard
Ranch Hand

Joined: Mar 30, 2012
Posts: 80

I have a GUI that creates an instance of another class that can create a JOptionPane under certain conditions to inform the user of something.

Is it best to pass in the JFrame to the constructor of that class so I can send that to the JOptionPane like so:

or do something like this with a static method:

The reason I want the GUI frame is so my dialog boxes pop up on top of that frame, instead of the center of the screen.

Perhaps there is another way of doing this?

I don't want to "clutter" up the constructor when the use of that reference may never be used.

Thanks for the help


I've got just enough Java knowledge to royally screw everything up. :-)
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4594

Sounds like you're mixing responsibilities.

What is a non-GUI class doing invoking a dialogue? You should adhere to the MVC principle. Maybe if you explain what the idea behind the dialogue and the middle-man class is, we can give you some advice.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Bd Howard
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
I appreciate the response Stephan.

I am currently in school earning a CS degree, and I have yet to even hear of MVC. I had an intro to OO course last year that used Java, and I've got one more OO course that uses C++ in the Spring. Perhaps MVC is covered in that course.

I am trying to learn Java so I'll have a skill when I leave school so that I can actually DO something.

Anyway, I'll read up on MVC and that may make this question I posted moot.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46320
I suggest you go through the documentation for JOptionPane and its methods. I think you will find that in most cases the first parameter sets the location on screen where the option pane dialogue appears. You will probably also find a Java Tutorials link, which I recommend you follow. It will help you a lot
I agree. Here's the link:
subject: Preferred/Best Practice
It's not a secret anymore!