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 Can I base all of my windows on JFrame? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can I base all of my windows on JFrame?" Watch "Can I base all of my windows on JFrame?" New topic

Can I base all of my windows on JFrame?

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I'm used to a class library (pre-Java, but object-oriented), that based all windows on a single base class. If written in Java, it would be like the following:

Is the above design possible in Swing, using the JFrame class? I'd like everything, deep-down to have a common anscestor class like JFrame. I'd rather do this than use three different classes:

JFrame for modeless windows.
JDialog for modal windows.
JOptionPane for simple alerts.

Is my design a good use of Swing or am I "going against the grain."

[ May 27, 2007: Message edited by: Kaydell Leavitt ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

All of these classes already have java.awt.Component as an ancestor; maybe you can make use of that fact.

But as far as top level windows go, I don't think there's any reason you're forced to use JDialog or JOptionPane, except for modality; off the top of my head I don't believe you can implement a true modal dialog with JFrame (someone may prove me wrong here, though.) If you can live without true modal dialogs, then there's no reason not to do what you suggest, other than the reinventing-the-wheel that' you'd be doing instead of using JOptionPane.

[Jess in Action][AskingGoodQuestions]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
. . . Go through the API and you will find that JWindow, JDialog and JFrame already inherit from Object in the same fashion.
I agree. Here's the link:
subject: Can I base all of my windows on JFrame?
It's not a secret anymore!