10.7 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 // import javax.swing.*; import java.awt.event.*; import java.awt.*; // A frame that can easily support internal frame dialogs public class DialogDesktop extends JFrame {   public DialogDesktop(String title) {     super(title);     setDefaultCloseOperation(EXIT_ON_CLOSE);     final JDesktopPane desk = new JDesktopPane( );     setContentPane(desk);     // Create our "real" application container; use any layout manager we want.     final JPanel p = new JPanel(new GridBagLayout( ));     // Listen for desktop resize events so we can resize p. This will ensure that     // our container always fills the entire desktop.     desk.addComponentListener(new ComponentAdapter( ) {       public void componentResized(ComponentEvent ev) {         Dimension deskSize = desk.getSize( );         p.setBounds(0, 0, deskSize.width, deskSize.height);         p.validate( );       }     });     // Add our application panel to the desktop. Any layer below the MODAL_LAYER     // (where the dialogs will appear) is fine. We'll just use the default in     // this example.     desk.add(p);     // Fill out our app with a few buttons that create dialogs.     JButton input = new JButton("Input");     JButton confirm = new JButton("Confirm");     JButton message = new JButton("Message");     p.add(input);     p.add(confirm);     p.add(message);     input.addActionListener(new ActionListener( ) {       public void actionPerformed(ActionEvent ev) {         JOptionPane.showInternalInputDialog(desk, "Enter Name");       }     });     confirm.addActionListener(new ActionListener( ) {       public void actionPerformed(ActionEvent ev) {         JOptionPane.showInternalConfirmDialog(desk, "Is this OK?");       }     });     message.addActionListener(new ActionListener( ) {       public void actionPerformed(ActionEvent ev) {         JOptionPane.showInternalMessageDialog(desk, "The End");       }     });   }   // A simple test program   public static void main(String[] args) {     DialogDesktop td = new DialogDesktop("Desktop");     td.setSize(350, 250);     td.setVisible(true);   } }

Most of this class is just a sample program proving that the strategy works. The key ideas come early on in the code. The first important thing is the creation of a JDesktopPane, which we set as the frame's content pane. We then add the "real" application container to the desktop. The last important detail is the little ComponentListener we add to the desktop pane to ensure that our main application container is resized when the size of the desktop changes. Figure 10-15 shows what the simple test program looks like after expanding it slightly, with two of the internal dialogs open.

Figure 10-15. DialogDesktop display
figs/swng2.1015.gif


Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net