Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 51 52 53 54 55 56 < 57 > 58 59 60 61 62 63 .. 166 >> Next

06
07 public class BadLayeredPane extends JFrame
08 {
09 public BadLayeredPane()
10 {
11 // Error 1: using the Root layered pane
12 JLayeredPane lp = getLayeredPane();
13
14 // set the size of this pane
15 lp.setPreferredSize(new Dimension(100,100));
16
17 // add a Colored Panel
18 JPanel jpnl = new JPanel();
19 jpnl.setSize(100,100);
20 jpnl.setOpaque(true);
21 jpnl.setBackground(Color.red);
22
23 // Error 2: these MUST be of type Integer.
24 lp.add(jpnl, 2);
25
26 // put a Button on top
27 Button b = new Button("Hi!");
28 // Error 3: adding button wrong
29 lp.add(b, 1);
30 }
31
32 public static void main(String [] args)
33 {
34 JFrame frame = new BadLayeredPane();
35
36 frame.addWindowListener(
37 new WindowAdapter()
38 {
39 public void windowClosing(WindowEvent e)
40 {
41 System.exit(0);
42 }
43 });
44
45 frame.pack();
46 frame.setVisible(true);
47 }
48 }
49
Listing 19.1 BadLayeredPane.java
148 Item 19
Figure 19.2 Run of BadLayeredPane.
When Listing 19.1 runs, it produces the screen in Figure 19.2.
Not only is our JLayeredPane not working properly, it has no size! We must first work through the size problem before we can approach the heart of our pitfall. Listing
19.1 features three errors (called out in the comments); I'll tackle the first two now and address the third later. First, the JLayeredPane that is part of the JFrame's JRootPane causes our size problem. When you examine the source code for JRootPane, you see that the JRootPane's RootLayout does not use the JLayeredPane to calculate its size; JLayeredPane only calculates the size of the content pane and the menu bar. Second, when adding components to our JLayeredPane, we use integers instead of Integer objects.
With this knowledge, let's examine our second attempt at displaying our two simple layers. Listing 19.2 fixes two of our problems.
01 package org.javapitfalls.item19;
02
03 import java.awt.*;
04 import javax.swing.*;
05 import java.awt.event.*;
06
07 public class BadLayeredPane2 extends JFrame
08 {
09 public BadLayeredPane2()
10 {
11 // Fix 1: Create a JLayeredPane
12 JLayeredPane lp = new JLayeredPane();
13
14 // set the size of this pane
15 lp.setPreferredSize(new Dimension(100,100));
16
17 // add a Colored Panel
18 JPanel jpnl = new JPanel();
19 jpnl.setSize(100,100);
20 jpnl.setOpaque(true);
21 jpnl.setBackground(Color.red);
22
23 // Fix 2: using Integer objects
24 lp.add(jpnl, new Integer(2));
Listing 19.2 BadLayeredPane2.java
JLayered Pane Pitfalls 149
25
26 // put a Button on top
27 Button b = new Button("Hi!");
28 lp.add(b, new Integer(1));
29
30 // Part of Fix 1
31 getContentPane().add(lp);
32 }
33
// main method() Identical to BadLayeredPane.java
50 }
Listing 19.2 (continued)
We'll first study the fixes applied and then the results. There are two fixes in Listing 19.2 ( called out in the comments):
First, we create a new JLayeredPane, which we add to the ContentPane.
The RootLayout manager uses the ContentPane to calculate the frame's size, so now the JFrame is packed properly.
Second, we correctly add components to the JLayeredPane using an Integer object to specify the layer.
Figure 19.3 shows the result of these fixes.
Figure 19.3 clearly demonstrates that we have not yet accomplished our goal. Though the colored panel displays, the button fails to appear on the layer above the panel. Why? Because we assume we add components to a JLayeredPane the same way we add components to Frames and Panels. This assumption is our third error and the JLayeredPane pitfall. Contrary to Frame and Panel, the JLayeredPane lacks a default LayoutManager; thus, the components have no sizes or positions provided for them by default. Instead, a component's size and position must be explicitly set before adding them to the JLayeredPane, which Fix 1 achieves in Listing 19.3.
Figure 19.3 Run of BadLayeredPane2.
150 Item 19
01 package org.javapitfalls.item19;
02
03 import java.awt.*;
04 import javax.swing.*;
05 import java.awt.event.*;
06
07 public class GoodLayeredPane extends JFrame
08 {
09 public GoodLayeredPane()
10 {
11 JLayeredPane lp = new JLayeredPane();
12
13 // set the size of this pane
14 lp.setPreferredSize(new Dimension(100,100));
15
16 // add a Colored Panel
17 JPanel jpnl = new JPanel();
18 jpnl.setSize(100,100);
19 jpnl.setOpaque(true);
20 jpnl.setBackground(Color.red);
21
22 lp.add(jpnl, new Integer(1));
23
24 // put a Button on top
25 Button b = new Button("Hi!");
26 // Fix 1: set the size and position
27 b.setBounds(10,10, 80, 40);
28 lp.add(b, new Integer(2));
29
30 getContentPane().add(lp);
31 }
32
// main() method Identical to BadLayeredPane.java
49 }
Listing 19.3 GoodLayeredPane.java
When run, Listing 19.3 produces the correct result, shown in Figure 19.4.
Figure 19.4 Run of GoodLayeredPane.
When File.renameTo() Won't 151
In summary, the key pitfall in our JLayeredPane example is wrongly assuming that the JLayeredPane has a default LayoutManager like JFrame and JPanel. Experience tells us to eliminate that assumption and position and size the components for each layer. Once we do so, the JLayeredPane works fine.
Previous << 1 .. 51 52 53 54 55 56 < 57 > 58 59 60 61 62 63 .. 166 >> Next