Mixing Heavyweight and Lightweight Components

   

Mixing Heavyweight and Lightweight Components

In Chapter 14, "Building a GUI: AWT Style," you learned that it's not a good idea to mix AWT heavyweight components with lightweight Swing components. Now, you're going to see what happens when this advice is ignored. To demonstrate what can go wrong, several Swing programs will be presented. Don't be concerned about not understanding their collective source code. After you've read the next chapter, come back and study this code. You'll find that it makes more sense.

The first program ” MixedDemo1 ” demonstrates what happens when a lightweight label is placed into the same container as a heavyweight label. The result is shown in Figure 15.3 and source code is presented in Listing 15.4.

Figure 15.3. MixedDemo1 's GUI shows a heavyweight label overlapping a lightweight label.

graphics/15fig03.gif

Listing 15.4 The MixedDemo1 Application Source Code
 // MixedDemo1.java import java.awt.*; import java.awt.event.*; import javax.swing.*; class MixedDemo1 extends JFrame {    MixedDemo1 (String title)    {       super (title);       addWindowListener (new WindowAdapter ()                          {                              public void windowClosing (WindowEvent e)                              {                                 System.exit (0);                              }                          });       JPanel jp = new JPanel ();       JLabel jl = new JLabel ("lightweight labels don't overlap");       jp.add (jl);       getContentPane ().add (jp, BorderLayout.SOUTH);       jp = new JPanel ();       Label l = new Label ("heavyweight labels overlap");       l.setBackground (Color.white);       jp.add (l);       getContentPane ().add (jp, BorderLayout.CENTER);       setSize (200, 70);       setVisible (true);    }    public static void main (String [] args)    {       new MixedDemo1 ("Mixed Demo1");    } } 

The second program ” MixedDemo2 ” demonstrates what happens when a lightweight component and a heavyweight component are placed into the same container, and then this container is added to a Swing scroll pane container. The result is shown in Figure 15.4 and source code is presented in Listing 15.5.

Figure 15.4. MixedDemo2 's GUI shows a heavyweight button overlapping a scroll pane and scrollbars.

graphics/15fig04.gif

Listing 15.5 The MixedDemo2 Application Source Code
 // MixedDemo2.java import java.awt.*; import java.awt.event.*; import javax.swing.*; class MixedDemo2 extends JFrame {    MixedDemo2 (String title)    {       super (title);       addWindowListener (new WindowAdapter ()                          {                              public void windowClosing (WindowEvent e)                              {                                 System.exit (0);                              }                          });       JPanel jp = new JPanel ();       jp.setLayout (new BorderLayout ());       JPanel jp1 = new JPanel ();       JButton jb = new JButton ("lightweight button doesn't overlap");       jp1.add (jb);       jp.add (jp1, BorderLayout.NORTH);       jp1 = new JPanel ();       Button b = new Button ("heavyweight button overlaps");       jp1.add (b);       jp.add (jp1, BorderLayout.SOUTH);       JScrollPane jsp = new JScrollPane (jp);       getContentPane ().add (jsp);       setSize (175, 100);       setVisible (true);    }    public static void main (String [] args)    {       new MixedDemo2 ("Mixed Demo2");    } } 

The third program ” MixedDemo3 ” demonstrates what happens when a lightweight component is placed into a Swing internal frame container; a heavyweight component is placed into a second internal frame; both frames are added to a Swing desktop pane container; both frames overlap; and the frame with the lightweight component is shown in front of the frame with the heavyweight component. The result is shown in Figure 15.5 and source code is presented in Listing 15.6. (If you compile and run this program, you'll have to drag the internal frame with the lightweight component over the internal frame with the heavyweight component, to see the heavyweight component overlapping the internal frame with the lightweight component.)

Figure 15.5. MixedDemo3 's GUI shows a heavyweight component in an internal frame overlapping a lightweight component in another internal frame.

graphics/15fig05.gif

Listing 15.6 The MixedDemo3 Application Source Code
 // MixedDemo3.java import java.awt.*; import java.awt.event.*; import javax.swing.*; class MixedDemo3 extends JFrame {    JDesktopPane desktop;    MixedDemo3 (String title)    {      super (title);      addWindowListener (new WindowAdapter ()                         {                             public void windowClosing (WindowEvent e)                             {                                System.exit (0);                             }                         });       desktop = new JDesktopPane ();       JInternalFrame jf1 = new JInternalFrame ("Frame #1");       JLabel jl = new JLabel ("lightweight components don't overlap");       jf1.getContentPane ().add (jl);       jf1.setSize (250, 100);       jf1.setVisible (true);       desktop.add (jf1);       JInternalFrame jf2 = new JInternalFrame ("Frame #2");       Label l = new Label ("heavyweight components overlap");       jf2.getContentPane ().add (l);       jf2.setSize (250, 100);       jf2.setVisible (true);       desktop.add (jf2);  setContentPane (desktop);       desktop.setVisible (true);       setSize (400, 200);       setVisible (true);    }    public static void main (String [] args)    {       new MixedDemo3 ("Mixed Demo3");    } } 

Finally, the fourth program ” MixedDemo4 ” demonstrates what happens when a lightweight Swing combo box component and a heavyweight component are placed into the same container, and the combo box's pop-up menu is displayed. The result is shown in Figure 15.6 and source code is presented in Listing 15.7.

Figure 15.6. MixedDemo4 's GUI shows a heavyweight button overlapping a combo box's pop-up menu.

graphics/15fig06.gif

Listing 15.7 The MixedDemo4 Application Source Code
 // MixedDemo4.java import java.awt.*; import java.awt.event.*; import javax.swing.*; class MixedDemo4 extends JFrame {    MixedDemo4 (String title)    {       super (title);       addWindowListener (new WindowAdapter ()                          {                              public void windowClosing (WindowEvent e)                              {                                 System.exit (0);                              }                          });       JPanel p = new JPanel (); // JPopupMenu.setDefaultLightWeightPopupEnabled (false);       JComboBox cb = new JComboBox (); // cb.setLightWeightPopupEnabled (false);       cb.addItem ("First");       cb.addItem ("Second");       cb.addItem ("Third");       p.add (cb);       getContentPane ().add (p, BorderLayout.NORTH);       p = new JPanel ();       p.add (new Button ("Heavyweight"));       getContentPane ().add (p, BorderLayout.CENTER);       setSize (200, 125);       setVisible (true);    }    public static void main (String [] args)    {       new MixedDemo4 ("Mixed Demo4");    } }  

In MixedDemo4, two lines are commented out. If you uncomment either line, recompile, and run the program, you'll discover that the heavyweight button does not overlap the lightweight combo box when its pop-up menu is displayed.

Any Swing component that displays a pop-up menu (such as a combo box) has a Boolean lightWeightPopupEnabled property. If this property is true (the default), the pop-up menu component is placed in a lightweight Swing container. When activated, any heavyweight component in the vicinity can overlap this pop-up menu. However, when this property is set to false, the pop-up menu is placed in a heavyweight panel container. The heavyweight component can no longer overlap the pop-up menu. You can set this property to true for all pop-up menu components by calling JPopupMenu 's setDefaultLightWeightPopupEnabled class method. If you're more concerned with a single pop-up menu, you can call the component's setLightWeightPopupEnabled method.

Although it's usually not a good idea to mix lightweight and heavyweight components, you might not always have a choice. If you are very careful, it's possible to successfully mix these components. MixedHello demonstrates this successful mixing by combining lightweight and heavyweight buttons into a single GUI. The result is shown in Figure 15.7 and source code is presented in Listing 15.8.

Figure 15.7. MixedHello 's GUI shows a mixture of heavyweight and lightweight button components.

graphics/15fig07.gif

Listing 15.8 The MixedHello Application Source Code
 // MixedHello.java import java.awt.*; import javax.swing.*; import java.awt.event.*; class MixedHello extends JFrame implements ActionListener {    Button b1;    JButton b2;    static String [] labels =    {       "Hello! Welcome to mixed Swing!",       "This program mixes an AWT button with a Swing button.",       "You cannot assign icons to AWT buttons.",       "You can assign icons to Swing buttons."    };    int nextLabel;    MixedHello (String title)    {       super (title);       addWindowListener (new WindowAdapter ()                          {                              public void windowClosing (WindowEvent e)                              {                                 System.exit (0);                              }                          });       Panel p = new Panel ();       b1 = new Button ("AWT button");       b1.addActionListener (this);       p.add (b1);       b2 = new JButton (labels [nextLabel++],                        new ImageIcon ("bullet.gif"));       b2.addActionListener (this);       p.add (b2);       setContentPane (p);       setSize (400, 100);       setVisible (true);    }    public void actionPerformed (ActionEvent e)    {       b2.setText (labels [nextLabel++]);       nextLabel %= labels.length;    }    public static void main (String [] args)    {       new MixedHello ("Hello, Mixed Version");    } } 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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