Book Home Java Enterprise in a Nutshell Search this book

Chapter 24. The javax.swing.border Package

The javax.swing.border package is a simple package that defines the Border interface, which specifies how to draw a border around an arbitrary Swing component. The various classes in this package implement commonly used border styles. Applications can easily define custom border styles by creating custom implementations of Border. Figure 24-1 shows the class hierarchy of this package.

figure

Figure 24-1. The javax.swing.border package

AbstractBorderJava 1.2
javax.swing.borderserializable

This abstract class implements a zero-width, nonopaque border. AbstractBorder implements the Border interface. To create a custom border type, subclass AbstractBorder, reimplementing at least the getBorderInsets() and paintBorder() methods. getInteriorRectangle() in its static and nonstatic versions is a convenience method that returns a rectangle that represents the region of the component inside the border.

AbstractBorder provides a two-argument version of getBorderInsets(), in addition to the one-argument version required by Border. The two-argument version should place the insets in the supplied Insets object. Doing so does not require a new Insets object to be created, so it is more efficient than the one-argument version. Swing components use this more efficient method whenever possible.

public abstract class AbstractBorder implements Border, Serializable {
// Public Constructors
public AbstractBorder ();
// Public Class Methods
public static java.awt.Rectangle getInteriorRectangle (Component c, Border b, int x, int y, int width, int height);
// Public Instance Methods
public java.awt.Insets getBorderInsets (Component c, java.awt.Insets insets);
public java.awt.Rectangle getInteriorRectangle (Component c, int x, int y, int width, int height);
// Methods Implementing Border
public java.awt.Insets getBorderInsets (Component c);
public boolean isBorderOpaque (); constant
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height); empty
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)

Subclasses: BevelBorder, CompoundBorder, EmptyBorder, EtchedBorder, LineBorder, TitledBorder

BevelBorderJava 1.2
javax.swing.borderserializable

This class displays a two-pixel-wide beveled edge around a Swing component, giving the appearance of a raised or lowered surface. The RAISED and LOWERED constants specify the type of bevel. It usually is not necessary to specify bevel colors, as appropriate defaults are derived from the component background color. See also the createBevelBorder(), createLoweredBevelBorder(), and createRaisedBevelBorder() methods of javax.swing.BorderFactory.

public class BevelBorder extends AbstractBorder {
// Public Constructors
public BevelBorder (int bevelType);
public BevelBorder (int bevelType, java.awt.Color highlight, java.awt.Color shadow);
public BevelBorder (int bevelType, java.awt.Color highlightOuter, java.awt.Color highlightInner, java.awt.Color shadowOuter, java.awt.Color shadowInner);
// Public Constants
public static final int LOWERED ; =1
public static final int RAISED ; =0
// Public Instance Methods
public int getBevelType ();
public java.awt.Color getHighlightInnerColor (Component c);
public java.awt.Color getHighlightOuterColor (Component c);
public java.awt.Color getShadowInnerColor (Component c);
public java.awt.Color getShadowOuterColor (Component c);
// Public Methods Overriding AbstractBorder
public java.awt.Insets getBorderInsets (Component c);
public java.awt.Insets getBorderInsets (Component c, java.awt.Insets insets);
public boolean isBorderOpaque (); constant
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height);
// Protected Instance Methods
protected void paintLoweredBevel (Component c, java.awt.Graphics g, int x, int y, int width, int height);
protected void paintRaisedBevel (Component c, java.awt.Graphics g, int x, int y, int width, int height);
// Protected Instance Fields
protected int bevelType ;
protected java.awt.Color highlightInner ;
protected java.awt.Color highlightOuter ;
protected java.awt.Color shadowInner ;
protected java.awt.Color shadowOuter ;
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)-->BevelBorder

Subclasses: SoftBevelBorder, javax.swing.plaf.BorderUIResource.BevelBorderUIResource

BorderJava 1.2
javax.swing.border

This simple interface defines the methods that must be implemented by any object that wants to display a border around a Swing component. getBorderInsets() returns a java.awt.Insets object that specifies the size of each edge of the border. isBorderOpaque() indicates either that the border is opaque or that it allows the container background to show through. Opaque borders are required to paint all pixels in the border region. Finally, paintBorder() is the method that is responsible for drawing the border around the specified component at the specified location, using the specified java.awt.Graphics object.

The javax.swing.border package contains a number of Border implementations that are useful with Swing classes. Note, however, that Border instances are intended to be shared among components. This means that you typically should obtain references to shared Border objects by calling the static factory methods of javax.swing.BorderFactory. To define your own custom type of border, you can subclass AbstractBorder.

public abstract interface Border {
// Public Instance Methods
public abstract java.awt.Insets getBorderInsets (Component c);
public abstract boolean isBorderOpaque ();
public abstract void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height);
}

Implementations: AbstractBorder, javax.swing.plaf.BorderUIResource

Passed To: Too many methods to list.

Returned By: Too many methods to list.

Type Of: DefaultListCellRenderer.noFocusBorder, CompoundBorder.{insideBorder, outsideBorder}, TitledBorder.border, javax.swing.table.DefaultTableCellRenderer.noFocusBorder, javax.swing.tree.DefaultTreeCellEditor.DefaultTextField.border

CompoundBorderJava 1.2
javax.swing.borderserializable

This class combines two borders and displays the result around a Swing component. For example, you can use CompoundBorder to combine a beveled outer border and an etched inner border. Or you can use CompoundBorder to combine a MatteBorder and an EmptyBorder, to create an additional margin within the MatteBorder. See also the createCompoundBorder() method of javax.swing.BorderFactory.

public class CompoundBorder extends AbstractBorder {
// Public Constructors
public CompoundBorder ();
public CompoundBorder (Border outsideBorder, Border insideBorder);
// Property Accessor Methods (by property name)
public boolean isBorderOpaque (); Overrides:AbstractBorder default:false
public Border getInsideBorder (); default:null
public Border getOutsideBorder (); default:null
// Public Methods Overriding AbstractBorder
public java.awt.Insets getBorderInsets (Component c);
public java.awt.Insets getBorderInsets (Component c, java.awt.Insets insets);
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height);
// Protected Instance Fields
protected Border insideBorder ;
protected Border outsideBorder ;
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)-->CompoundBorder

Subclasses: javax.swing.plaf.BorderUIResource.CompoundBorderUIResource

Returned By: BorderFactory.createCompoundBorder()

EmptyBorderJava 1.2
javax.swing.borderserializable

This class implements a transparent, empty border. It is used to place a blank margin around a Swing component. The arguments to the constructor specify the number of pixels of blank space to appear on each edge of the component. See also the createEmptyBorder() method of javax.swing.BorderFactory.

public class EmptyBorder extends AbstractBorder implements Serializable {
// Public Constructors
public EmptyBorder (java.awt.Insets insets);
public EmptyBorder (int top, int left, int bottom, int right);
// Public Methods Overriding AbstractBorder
public java.awt.Insets getBorderInsets (Component c);
public java.awt.Insets getBorderInsets (Component c, java.awt.Insets insets);
public boolean isBorderOpaque (); constant
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height); empty
// Protected Instance Fields
protected int bottom ;
protected int left ;
protected int right ;
protected int top ;
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)-->EmptyBorder(Serializable)

Subclasses: MatteBorder, javax.swing.plaf.BorderUIResource.EmptyBorderUIResource

EtchedBorderJava 1.2
javax.swing.borderserializable

This class displays an etched border around a Swing component. By default, the border appears etched into the screen. You can create a border that appears etched out by passing the RAISED constant to the EtchedBorder() constructor. It usually is not necessary to specify colors for the border, since EtchedBorder automatically chooses correct defaults based on the component's background color. See also the createEtchedBorder() method of javax.swing.BorderFactory.

public class EtchedBorder extends AbstractBorder {
// Public Constructors
public EtchedBorder ();
public EtchedBorder (int etchType);
public EtchedBorder (java.awt.Color highlight, java.awt.Color shadow);
public EtchedBorder (int etchType, java.awt.Color highlight, java.awt.Color shadow);
// Public Constants
public static final int LOWERED ; =1
public static final int RAISED ; =0
// Public Instance Methods
public int getEtchType (); default:1
public java.awt.Color getHighlightColor (Component c);
public java.awt.Color getShadowColor (Component c);
// Public Methods Overriding AbstractBorder
public java.awt.Insets getBorderInsets (Component c);
public java.awt.Insets getBorderInsets (Component c, java.awt.Insets insets);
public boolean isBorderOpaque (); constant default:true
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height);
// Protected Instance Fields
protected int etchType ;
protected java.awt.Color highlight ;
protected java.awt.Color shadow ;
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)-->EtchedBorder

Subclasses: javax.swing.plaf.BorderUIResource.EtchedBorderUIResource

LineBorderJava 1.2
javax.swing.borderserializable

This class draws a solid line of the specified color and thickness around a Swing component. The default thickness is one pixel. The two static methods return shared LineBorder instances that draw black and gray lines that are one-pixel wide. See also the createLineBorder() method of javax.swing.BorderFactory.

public class LineBorder extends AbstractBorder {
// Public Constructors
public LineBorder (java.awt.Color color);
public LineBorder (java.awt.Color color, int thickness);
// Public Class Methods
public static Border createBlackLineBorder ();
public static Border createGrayLineBorder ();
// Public Instance Methods
public java.awt.Color getLineColor ();
public int getThickness ();
// Public Methods Overriding AbstractBorder
public java.awt.Insets getBorderInsets (Component c);
public java.awt.Insets getBorderInsets (Component c, java.awt.Insets insets);
public boolean isBorderOpaque (); constant
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height);
// Protected Instance Fields
protected java.awt.Color lineColor ;
protected boolean roundedCorners ;
protected int thickness ;
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)-->LineBorder

Subclasses: javax.swing.plaf.BorderUIResource.LineBorderUIResource

MatteBorderJava 1.2
javax.swing.borderserializable

This class uses a solid color or tiled icon to paint a border around a Swing component. The sizes of each edge of the border can be independently specified. If an icon is specified and border sizes are not, the top and bottom insets of the border equal the icon height, and the left and right insets equal the icon width. See also the createMatteBorder() method of javax.swing.BorderFactory.

public class MatteBorder extends EmptyBorder {
// Public Constructors
public MatteBorder (Icon tileIcon);
public MatteBorder (int top, int left, int bottom, int right, Icon tileIcon);
public MatteBorder (int top, int left, int bottom, int right, java.awt.Color color);
// Public Methods Overriding EmptyBorder
public java.awt.Insets getBorderInsets (Component c);
public java.awt.Insets getBorderInsets (Component c, java.awt.Insets insets);
public boolean isBorderOpaque ();
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height);
// Protected Instance Fields
protected java.awt.Color color ;
protected Icon tileIcon ;
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)-->EmptyBorder(Serializable)-->MatteBorder

Subclasses: javax.swing.plaf.BorderUIResource.MatteBorderUIResource

Returned By: BorderFactory.createMatteBorder()

SoftBevelBorderJava 1.2
javax.swing.borderserializable

This class displays a two-pixel-wide raised or lowered beveled border around a Swing component. SoftBevelBorder differs from its superclass BevelBorder in that it draws a "softer" bevel (i.e., a bevel whose corners do not appear as sharp). Unlike the other border types, shared SoftBevelBorder instances cannot be created through the javax.swing.BorderFactory class.

public class SoftBevelBorder extends BevelBorder {
// Public Constructors
public SoftBevelBorder (int bevelType);
public SoftBevelBorder (int bevelType, java.awt.Color highlight, java.awt.Color shadow);
public SoftBevelBorder (int bevelType, java.awt.Color highlightOuter, java.awt.Color highlightInner, java.awt.Color shadowOuter, java.awt.Color shadowInner);
// Public Methods Overriding BevelBorder
public java.awt.Insets getBorderInsets (Component c);
public boolean isBorderOpaque (); constant
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height);
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)-->BevelBorder-->SoftBevelBorder

TitledBorderJava 1.2
javax.swing.borderserializable

This class combines a textual title with another border. This can be a useful effect when, for example, you want to group and title several components, such as a group of JRadioButton objects. By default, TitledBorder draws an EtchedBorder and displays the title left justified on the top edge of that border. Arguments to the constructor and property- setting methods allow you to specify the Border to be drawn, the position of the title relative to the top or bottom of that border, the justification of the title, the title font, and the title color. The title can be positioned above, on top of, or below the top or bottom edge of the border, and it can be left, center, or right justified. The positioning is specified by the titlePosition and titleJustification properties. TitledBorder constants define the legal values of these properties.

TitledBorder instances are not typically shared among components because different components do not typically have the same title. Since the TitledBorder instances are not shared, they need not be immutable. TitledBorder is unique among the classes in javax.swing.border in that it has properties that can be set after the instance is created. You can still use the createTitledBorder() method of javax.swing.BorderFactory to create TitledBorder instances, but since they are unlikely to be shared, this is typically not very useful.

public class TitledBorder extends AbstractBorder {
// Public Constructors
public TitledBorder (Border border);
public TitledBorder (String title);
public TitledBorder (Border border, String title);
public TitledBorder (Border border, String title, int titleJustification, int titlePosition);
public TitledBorder (Border border, String title, int titleJustification, int titlePosition, java.awt.Font titleFont);
public TitledBorder (Border border, String title, int titleJustification, int titlePosition, java.awt.Font titleFont, java.awt.Color titleColor);
// Public Constants
public static final int ABOVE_BOTTOM ; =4
public static final int ABOVE_TOP ; =1
public static final int BELOW_BOTTOM ; =6
public static final int BELOW_TOP ; =3
public static final int BOTTOM ; =5
public static final int CENTER ; =2
public static final int DEFAULT_JUSTIFICATION ; =0
public static final int DEFAULT_POSITION ; =0
public static final int LEFT ; =1
public static final int RIGHT ; =3
public static final int TOP ; =2
// Protected Constants
protected static final int EDGE_SPACING ; =2
protected static final int TEXT_INSET_H ; =5
protected static final int TEXT_SPACING ; =2
// Property Accessor Methods (by property name)
public Border getBorder ();
public void setBorder (Border border);
public boolean isBorderOpaque (); Overrides:AbstractBorder constant
public String getTitle ();
public void setTitle (String title);
public java.awt.Color getTitleColor ();
public void setTitleColor (java.awt.Color titleColor);
public java.awt.Font getTitleFont ();
public void setTitleFont (java.awt.Font titleFont);
public int getTitleJustification ();
public void setTitleJustification (int titleJustification);
public int getTitlePosition ();
public void setTitlePosition (int titlePosition);
// Public Instance Methods
public java.awt.Dimension getMinimumSize (Component c);
// Public Methods Overriding AbstractBorder
public java.awt.Insets getBorderInsets (Component c);
public java.awt.Insets getBorderInsets (Component c, java.awt.Insets insets);
public void paintBorder (Component c, java.awt.Graphics g, int x, int y, int width, int height);
// Protected Instance Methods
protected java.awt.Font getFont (Component c);
// Protected Instance Fields
protected Border border ;
protected String title ;
protected java.awt.Color titleColor ;
protected java.awt.Font titleFont ;
protected int titleJustification ;
protected int titlePosition ;
}

Hierarchy: Object-->AbstractBorder(Border,Serializable)-->TitledBorder

Subclasses: javax.swing.plaf.BorderUIResource.TitledBorderUIResource

Returned By: BorderFactory.createTitledBorder()



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.