< Day Day Up > |
The JPanel [103] class provides general-purpose containers for lightweight components . By default, panels don't paint anything except their background; however, you can easily add borders to them. Borders are covered in How to Use Borders (page 535) in Chapter 9.
In many look and feels (but not GTK+), panels are opaque by default. Opaque panels work well as content panes and can help painting efficiency, as described in Chapter 6. You can toggle a panel's transparency by invoking setOpaque . A transparent panel draws no background, so any components underneath show through. An ExampleFigure 38 shows a shaded version of the Converter application, which is discussed in more detail in Using Models (page 50). Figure 38. A screenshot of the Converter applications with several of its JPanel s shaded.
Converter uses panels in several ways (as shown in Figure 39):
Figure 39. A screenshot of what Converter normally looks like (without shading).
As Converter demonstrates , panels are useful for grouping components, simplifying component layout, and putting borders around groups of components. The rest of this section gives hints on grouping and laying out components. For information about using borders, see How to Use Borders (page 535) in Chapter 9. Setting the Layout ManagerLike other containers, a panel uses a layout manager to position and size its components. By default, a panel's layout manager is an instance of FlowLayout , which places the panel's contents in a row. You can easily make a panel use any other layout manager by invoking the setLayout method or specifying a layout manager when creating the panel. The latter approach is preferable for performance reasons, since it avoids the unnecessary creation of a FlowLayout object. Here's an example of setting the layout manager when creating the panel: JPanel p = new JPanel(new BorderLayout()); //PREFERRED! This approach doesn't work with BoxLayout , since the BoxLayout constructor requires a pre-existing container. Here's an example that uses BoxLayout : JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS)); Adding ComponentsWhen you add components to a panel, you use the add method. Exactly which arguments you specify to the add method depend on which layout manager the panel uses. When the layout manager is FlowLayout , BoxLayout , GridLayout , or SpringLayout , you'll typically use the one-argument add method, like this: aFlowPanel.add(aComponent); aFlowPanel.add(anotherComponent); When the layout manager is BorderLayout , you need to provide an argument specifying the added component's position within the panel. For example: aBorderPanel.add(aComponent, BorderLayout.CENTER); aBorderPanel.add(anotherComponent, BorderLayout.PAGE_END); With GridBagLayout you can use either add method, but you must somehow specify grid bag constraints for each component. For information about using the standard layout managers, see Chapter 4, Laying Out Components within a Container (page 87). The Panel APIThe API in the JPanel class itself is minimal. The methods you are most likely to invoke on a JPanel object are those it inherits from its superclasses ” JComponent , Container , and Component . Tables 52 through 54 list the API you're most likely to use, with the exception of methods related to borders and layout hints. For more information about the API all JComponent s can use, see The JComponent Class (page 53). Also refer to the JPanel API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JPanel.html. Table 52. Creating a JPanel
Table 53. Managing a Container's Components
Table 54. Setting/Getting the Layout Manager
Examples That Use PanelsMany examples in this book use JPanel objects. The following table lists a few.
|
< Day Day Up > |