Java Reference
In-Depth Information
JSeparator Class
The JSeparator class is a special component that acts as a separator on a JMenu . The JPopupMenu
and JToolBar classes also support separators, but each uses its own subclass of JSeparator . In
addition to being placed on a menu, the JSeparator can be used anywhere you want to use a
horizontal or vertical line to separate different areas of a screen.
The JSeparator is strictly a visual component; therefore, it has no data model.
Creating JSeparator Components
To create a separator for a JMenu , you don't directly create a JSeparator , although you can.
Instead, you call the addSeparator() method of JMenu , and the menu will create the separator
and add the separator as its next item. The fact that it's a JSeparator (which isn't a JMenuItem
subclass) is hidden. There's also an insertSeparator(int index) method of JMenu that allows
you to add a separator at a specific position on the menu, that isn't necessarily the next slot.
If you plan to use a JSeparator away from a menu (for example, to visually separate two
panels in a layout), you should use one of the two constructors for JSeparator :
public JSeparator()
JSeparator jSeparator = new JSeparator();
public JSeparator(int orientation)
JSeparator jSeparator = new JSeparator(JSeparator.VERTICAL);
These constructors allow you to create a horizontal or vertical separator. If an orientation
isn't specified, the orientation is horizontal. If you want to explicitly specify an orientation, you
use either of the JSeparator constants of HORIZONTAL and VERTICAL .
JSeparator Properties
After you have a JSeparator , you add it to the screen like any other component. The initial
dimensions of the component are empty (zero width and height), so if the layout manager of
the screen asks the component what size it would like to be, the separator will reply that it
needs no space. On the other hand, if the layout manager offers a certain amount of space, the
separator will use the space if the orientation is appropriate. For instance, adding a horizontal
JSeparator to the north side of a BorderLayout panel draws a separator line across the screen.
However, adding a horizontal JSeparator to the east side of the same panel would result in
nothing being drawn. For a vertical JSeparator , the behavior is reversed: The north side would
be empty and a vertical line would appear on the east side.
The four properties of JSeparator are listed in Table 6-7.
Table 6-7. JSeparator Properties
Property Name
Data Type
Access
accessibleContext
AccessibleContext
Read-only
orientation
int
Read-write bound
UI
SeparatorUI
Read-write bound
UIClassID
String
Read-only
Search WWH ::




Custom Search