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 JDialog (it can be set using the constructor). I believe that there is no β€œofficial” opportunity (other than hacking), but I wanted to make sure that I did not miss anything. Any ideas or hints on this topic would be helpful, thanks already ...

+6
java swing jdialog
source share
3 answers

If you have a question about how to reuse dialogs during the application life cycle, the best way is:

  • define all contents of your dialog as subclasses of JPanel
  • and instantiate a new JDialog with an existing instance of a subclass of JPanel

For point 2, you can, of course, use a lazy panel rating (create an instance the first time you use it, and then reuse it).

You will also need your panels to implement some interface (your own) that allows you to reinitialize them for reuse in the new JDialog (reinitializing usually means deleting the contents of all fields or changing these fields to their default values).

+7
source share

The only thing I can think of is unsafe hacking (use reflection and change the owner, but this can change in a different version of the JVM (even from the same version on the same platform)).

Perhaps the best question for you is "is this what I'm trying to do ... do I really need to change the owner of the dialogue or is there a better way"? I try to think about the reasons to want to change the owner, and I cannot think of ...

+1
source share

J

I had a similar need for one β€œglobal” dialog in my application to serve the β€œnotes” dialog when the text in the table cell is too long to display. In this case, the user can hover or right-click the cell and get a dialog box that displays all the text. However, I wanted the dialog to display quickly, especially when it hung, so I just wanted to show and hide it. I decided to create and save this dialog in the main class with the application frame as the owner. Then, whenever any view that was owned by a frame needs a notes dialog, the view can get it from the main class. In cases where the dialog box required a note dialog, the parent dialog could create a new instance of the note dialog with the appropriate owner. Since these parent dialogs (in my application, at least) are located after closing, an extra instance of the notes dialog goes beyond and collects garbage. Hope this helps.

+1
source share

All Articles