Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to change the owner of a JDialog?

I have a very specific problem, and I wanted to know if there is a way to change the owner of a JDialog (it can be set using the constructor). I suppose there is no "official" possibility (other than a hack), but I wanted to make sure I didn't miss something. Any ideas or hints on the topic would be helpful, thanks already...

like image 863
roesslerj Avatar asked Feb 16 '09 15:02

roesslerj


People also ask

What does JDialog do?

Creates a dialog with an empty title and the specified modality and Frame as its owner. If owner is null , a shared, hidden frame will be set as the owner of the dialog. This constructor sets the component's locale property to the value returned by JComponent.

What is Modal JDialog?

JDialog(Dialog owner, String title, boolean modal) Creates a dialog box with the specified Dialog owner, title, and modality.


2 Answers

If your question is about how to reuse dialogs during your application lifecycle, then a better way is to:

  1. define all your dialog contents as JPanel subclasses
  2. and instantiate a new JDialog with the existing JPanel subclass instance

For point 2, you can of course use lazy evaluation of the panels (instantiate upon first use only, then reuse).

You will also need to have your panels implement some interface (of your own) that allows you to re-initialize them for reuse in a new JDialog (reinit typically means erasing all fields contents, or setting these fields back to their default values).

like image 160
jfpoilpret Avatar answered Sep 21 '22 15:09

jfpoilpret


Only thing I can think of falls under unsafe hack (use reflection and alter the owner, but that could possibly change under different version of the JVM (even from the same vensor on the same platform)).

Perhaps a better question for you to ask is "this is what I am trying to do... do I really need to change the owner of the dialog or is there a better way"? I am trying to think of reasons to want to change the owner and I cannot come up with any...

like image 25
TofuBeer Avatar answered Sep 19 '22 15:09

TofuBeer