< Day Day Up > |
The JSeparator [128] class provides a horizontal or vertical dividing line or empty space. It's most commonly used in menus and tool bars. In fact, you can use separators without even knowing that a JSeparator class exists, since menus and tool bars provide convenience methods that create and add separators customized for their containers. Separators are somewhat similar to borders, except that they are genuine components and, as such, are painted inside a container rather than around the edges of a particular component.
The code to add the menu items and separators to a menu such as the one in Figure 58 is extremely simple, boiling down to something like this: menu.add(menuItem1); menu.add(menuItem2); menu.add(menuItem3); menu.addSeparator(); menu.add(rbMenuItem1); menu.add(rbMenuItem2); menu.addSeparator(); menu.add(cbMenuItem1); menu.add(cbMenuItem2); menu.addSeparator(); menu.add(submenu); Figure 58. A picture of a menu that has three separators, used to divide the menu into four groups of items.
Adding separators to a tool bar is similar. You can find the full code explained in the how-to sections for menus and tool bars. If you want more control over separators in menus and tool bars, you can directly use the JSeparator subclasses that implement them: JPopupMenu.Separator [129] and JToolBar.Separator . [130] In particular, JToolBar.Separator has API for specifying the separator's size .
Using JSeparatorYou can use the JSeparator class directly to provide a dividing line in any container (see Figure 59). Figure 59. This GUI has a separator to the right of the Fire button.
Separators have almost no API and are extremely easy to use as long as you keep one thing in mind: In most implementations , a vertical separator has a preferred height of 0, and a horizontal separator has a preferred width of 0. This means that a separator is not visible unless you either set its preferred size or put it in under the control of a layout manager such as BorderLayout or BoxLayout that stretches it to fill its available display area. The vertical separator does have a bit of width (and the horizontal a bit of height), so you should see some space where the separator is. However, the actual dividing line isn't drawn unless the width and height are both nonzero.
JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); buttonPane.add(fireButton); buttonPane.add(Box.createHorizontalStrut(5)); buttonPane.add(new JSeparator(SwingConstants.VERTICAL)); buttonPane.add(Box.createHorizontalStrut(5)); buttonPane.add(employeeName); buttonPane.add(hireButton); buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); As the code shows, the buttons , separator, and text field all share a single container ”a JPanel instance that uses a left-to-right box layout. Thanks to the layout manager (and to the fact that separators have unlimited maximum sizes), the separator is automatically made as tall as its available display area. In the preceding code, the horizontal struts are invisible components used to provide space around the separator. A 5-pixel empty border provides a cushion around the panel and also serves to prevent the separator from extending all the way to the component above it and the window's edge below it. Figure 60 shows another GUI that uses a separator, this time to put a dividing line between a group of controls and a display area. Figure 60. TextInputDemo uses a separator to divide two areas.
Here's the code that sets up the separator's container in TextInputDemo : [132]
JPanel panel = new JPanel(new BorderLayout()); ... panel.setBorder(BorderFactory.createEmptyBorder( GAP/2, //top 0, //left GAP/2, //bottom 0)); //right panel.add(new JSeparator(JSeparator.VERTICAL), BorderLayout.LINE_START); panel.add(addressDisplay, BorderLayout.CENTER); As in the last example, the panel uses an empty border so that the separator doesn't extend all the way to the edges of its container. Placing the separator in the leftmost area of the BorderLayout -controlled container makes the separator as tall as the address-display component that's in the center of the container. See How to Use BorderLayout (page 459) in Chapter 8 for details on how border layouts work. The Separator APIThe API for using separators (see Table 71) is minimal, since separators have no contents and don't respond to user input. See The JComponent API (page 55) for tables of commonly used inherited methods. See also the API documentation for JSeparator at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JSeparator.html. Table 71. Creating and Initializing Separators
Examples That Use SeparatorsSeveral of this chapter's examples use separators, usually in menus. Here's a list of some of the more interesting examples.
|
< Day Day Up > |