> I have a problem where I need to set the owner of a dialog after it has
> been constructed.
Sorry - you can't do that.

Signature
Regards,
John McGrath
> I have a problem where I need to set the owner of a dialog after it has
> been constructed. Looking at the API, it looks like the only place i
> can set the owner is in the super(Frame owner) or super(Dialog owner)
> constructors.
You can't do that, but ...
> I have a collection of dialogs, which are created using simple
> reflection (i.e. no constructors). I don't necessarily know what the
> parent window is at this point.
So don't create the dialogs at this point. If you absolutely must
construct a dialog's contents at this point, just create a JPanels with
all your components. When it is time to display the window, create a
JDialog with the desired parent, add the JPanel as content pane, pack(),
setVisible(true), and be done.
> Once they have all been constructed, I
> retrieve a handle to the window,
Why can't you first retrieve the reference (not "handle") to your
window? I would like to suggest that you rethink your application
architecture.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
laura p - 27 Jan 2005 09:48 GMT
Thank you for your suggestions, unfortunately a significant
architecture rethink is not possible.
My main application window is created by an existing large-scale
application. I am currently writing an additional component for this
application using JavaBeans and a third party library (which also uses
JavaBeans). BeanContext membership requires a default constructor.
This is called when the Beans are created - each bean is then given the
opportunity to utilise a handle (or reference ;) to every other bean in
that context. It is at this point that each dialog can discover what
its parent _should_ be, but I cannot set it at that point...
I did consider creating each component as a panel, then add it to a
dialog at a later point. However, any component that wants to affect
the dialog through an action (or retrieve its infomative contents) has
to get the reference to the actual dialog when the beans are created,
so this is not possible.
If it isn't possible to set the owner once the dialog has been created,
I will need to look at other ways of achieving the effect of being
owned by the main window...
L
Symon - 28 Jan 2005 08:10 GMT
You can also use a little workaround to attach your dialog with the
first visible frame :
private Frame findActiveFrame() {
Frame[] frames = JFrame.getFrames();
for (int i = 0; i < frames.length; i++) {
if (frames[i].isVisible()) {
return frames[i];
}
}
return null;
}
And create your dialog with : new JDialog(findActiveFrame());
Hope this helps.
> Thank you for your suggestions, unfortunately a significant
> architecture rethink is not possible.
[quoted text clipped - 19 lines]
>
> L