Using Internal Frame Dialogs with JDesktopPane
In order to get the best results when using internal frame
dialogs created by JOptionPane
, the
dialogs need to be placed in a JDesktopPane
. However, this may not be
convenient if your application does not use a JDesktopPane
. In this section, we’ll show how
you can easily adapt your application to use a JDesktopPane
so that you can use internal
frame dialogs.
Recall that JDesktopPane
has a
null
layout manager, leaving the
management of the location of its contents up to the DesktopManager
and the user. This makes
JDesktopPane
a poor choice when you
just need a container in which to build your main application. As a
result, if you want to have an internal frame dialog displayed in a
“normal” container, you need a solution that gives you the features of
both JDesktopPane
and a more
layout-friendly container.
This is actually a pretty straightforward goal to achieve. You
need to create a JDesktopPane
and add
your application container to it so that it fills an entire layer of the
desktop. When there are no internal frames displayed, this looks the
same as if you were displaying the application container alone. The
benefit is that when you need to add an internal frame dialog, you have
a desktop to add it to.
Here’s a simple example that shows how this works. It also shows how you can make sure your container fills the desktop, even if the desktop changes size (since there’s no layout manager, this won’t happen automatically).
// DialogDesktop.java ...
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.