Ain't JDialog tricky?

D

DeMarcus

Hi,

Why can't I set a title on a JDialog unless I give it an owner?

The problem is that I have a JPanel that fires the JDialog, but
how do I provide the JDialog with a valid owner? Somehow I need
to get hold of a Dialog or a Frame according to the JDialog's
constructors. But how can a JPanel get hold of that?

One thing that makes me believe there's a solution is the dirty
way of creating a JOptionPane and use the createDialog(). What's
that function doing behind the scenes?

Thanks.

~ Daniel
 
M

Michiel Konstapel

The problem is that I have a JPanel that fires the JDialog, but
how do I provide the JDialog with a valid owner? Somehow I need
to get hold of a Dialog or a Frame according to the JDialog's
constructors. But how can a JPanel get hold of that?

Just walk up getParent() recursively until you get a component that's a
(subclass of) Window or JWindow.
I think there's even a Swing utility method that does just that.
HTH,
Michiel
 
J

John McGrath

Just walk up getParent() recursively until you get a component that's a
(subclass of) Window or JWindow. I think there's even a Swing utility
method that does just that.

SwingUtilities.getWindowAncestor( Component ).
 
S

Steve W. Jackson

DeMarcus said:
:Hi,
:
:Why can't I set a title on a JDialog unless I give it an owner?
:
:The problem is that I have a JPanel that fires the JDialog, but
:how do I provide the JDialog with a valid owner? Somehow I need
:to get hold of a Dialog or a Frame according to the JDialog's
:constructors. But how can a JPanel get hold of that?
:
:One thing that makes me believe there's a solution is the dirty
:way of creating a JOptionPane and use the createDialog(). What's
:that function doing behind the scenes?
:
:Thanks.
:
: ~ Daniel

I'm not sure why you say you can't set the title without an owner. Of
course you can. It inherits setTitle from Dialog. And you can invoke
numerous constructors that take a title which will also allow a null
value for the owner frame or dialog.

Others have posted replies on getting the containing window of that
JPanel, but you should note that it inherits from JComponent the
getTopLevelAncestor method that returns the highest level ancestor of
itself. If it's in a window (which frames and dialogs all have as a
common ancestor class), it returns that. If it's in an applet, it
returns that.

= Steve =
 
D

DeMarcus

Steve said:
I'm not sure why you say you can't set the title without an owner. Of
course you can. It inherits setTitle from Dialog. And you can invoke
numerous constructors that take a title which will also allow a null
value for the owner frame or dialog.

Others have posted replies on getting the containing window of that
JPanel, but you should note that it inherits from JComponent the
getTopLevelAncestor method that returns the highest level ancestor of
itself. If it's in a window (which frames and dialogs all have as a
common ancestor class), it returns that. If it's in an applet, it
returns that.

= Steve =

Ok, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top