Swing border (1)

Source: Internet
Author: User

The Swing component provides the ability to customize the border area around the component. For simplicity, we can use predefined eight borders, or we can create our own borders. In this chapter, we will learn how to best use existing borders and how to create our own borders.

7.1 Some Basics on Woring with Borders
Border is the JComponent attribute with standard setBorder () and getBorder () attribute methods. Therefore, all the Swing components of the JComponent subclass have borders. By default, a widget does not have a custom border associated with it. (The getBorder () method of JComponent returns null .) On the contrary, the default border displayed by the component is the border most suitable for its status based on the current perception. For example, for a JButton, for each frame with a different perception, the border can be displayed as pressed, not pressed, or disabled.

Although the initial Border attribute of all components is set to null, we can call the setBorder (Border newValue) method of JComponent to modify the Border of the component. Once set, the modified value overwrites the current view border and draws a new border in the widget area. If we want to reset the border to a suitable border for the status and perception later, we can change the border attribute to null and use setBorder (null) and call the updateUI () method of the component. The updateUI () method will notify the viewer to reset the border. If the updateUI () method is not called, the component has no border.

Figure 7-1 shows the border settings around a JLabel. The text label is used to indicate the border type. How to create different borders will be discussed later in this chapter.

7.1.1 grouping ing the Border Inteface
We can find the border interface in the javax. swing. Border package. This interface forms the basis of all border classes. This interface is directly implemented by the AbstractBorder class, which is the parent class of all predefined Swing border classes: BevelBorder, CompoundBorder, EmptyBorder, EtchedBorder, LineBorder, MatteBorder, SoftBevelBorder, and TitledBorder. Another interesting class is the BorderFactory class. We can find this class in the javax. swing package. This class uses the factory design pattern to create borders, hiding implementation details, and caching various options to optimize joint use.

The Border interface displayed here consists of three methods: paintBorder (), getBordernsets (), and isBorderOpaque (). These methods are described in later sections.

PaintBorder ()

The paintBorder () method is the key method of this interface. It is defined as follows:

Public void paintBorder (Component c, Graphics g, int x, int y, int
Width, int height) This method is used to draw the border. Generally, the Border implementation first asks about the Insets dimension, and then draws the four edges in the outer area, as shown in 7-2. If the border is not transparent, the paintBorder () implementation must fill the entire internal area. If a border is not transparent and the area is not filled, This is a bug and needs to be corrected.

List 7-1 shows a simple paintBorder () Implementation, which uses a color that is slightly lighter than the upper and lower parts to fill the left and right sides.

Public void paintBorder (Component c, Graphics g, int x, int y, int width,
Int height ){
Insets insets = getBorderInsets (c );
Color color = c. getForeground ();
Color brighterColor = color. brighter ();
// Translate coordinate space
G. translate (x, y );
// Top
G. setColor (color );
G. fillRect (0, 0, width, insets. top );
// Left
G. setColor (brighterColor );
G. fillRect (0, insets. top, insets. left, height-insets.top-insets.bottom );
// Bottom
G. setColor (color );
G. fillRect (0, height-insets.bottom, width, insets. bottom );
// Right
G. setColor (brighterColor );
G. fillRect (width-insets.right, insets. top, insets. right,
Height-insets.top-insets.bottom );
// Translate coordinate space back
G. translate (-x,-y );
} When creating our own border, we often find ourselves filling in the same non-overlapping rectangular area. The translate () method of Graphics simplifies the coordinate assignment. You do not need to convert the coordinates. We need to offset the plot using the original (x, y.

Note: we cannot insert g. fillRect (x, y, width, height) to simplify the process, because this will fill the entire component area, not the border area.

GetBorderInsets ()

The getBorderInsets () method returns the space required to draw a border around the specified component c as The Insets object. It is defined as follows:

As shown in public Insets getBorderInsets (Component c) 7-2, these internal areas define valid areas where borders can be drawn. The Component parameter allows us to use some of its attributes to determine the size of the internal area.

IsBorderOpaque ()

The border can be opaque or transparent. The isBorderOpaque () method returns true or false to indicate the border format. It is defined as follows:

Public boolean isBorderOpaque () when this method returns true, the border needs to be non-transparent and fill its entire internal area. When false is returned, the background color of the component where the border is located is kept in the area not drawn.

7.1.2 Introducing BorderFactory
Now we have a basic understanding of how the Border interface works. Now let's take a look at the BorderFactory class as a simple method for creating borders. We can find this class in the javax. swing package. The BorderFactory class provides a series of static methods to create a pre-defined border. You do not need to call specific constructors of different border classes. With this factory class, you can create almost all borders. This factory class can cache the creation of some borders at the same time to avoid re-creating frequently used borders multiple times. The class is defined as follows:

Public class BorderFactory {
Public static Border createBevelBorder (int type );
Public static Border createBevelBorder (int type, Color highlight,
Color shadow );
Public static Border createBevelBorder (int type, Color highlightOuter,
Color highlightInner, Color shadowOuter, Color shadowInner );
Public static CompoundBorder createCompoundBorder ();
Public static CompoundBorder createCompoundBorder (Border outside,
Border inside );
Public static Border createEmptyBorder ();
Public static Border createEmptyBorder (int top, int left, int bottom,
Int right );
Public static Border createEtchedBorder ();
Public static Border createEtchedBorder (Color highlight, Color shadow );
Public static Border createEtchedBorder (int type );
Public static Border createEtchedBorder (int type, Color highlight,
Color shadow );
Public static Border createLineBorder (Color color );
Public static Border createLineBorder (Color color, int thickness );
Public static Border createLoweredBevelBorder ();
Public static MatteBorder createMatteBorder (int top, int left, int bottom,
Int right, Color color );
Public static MatteBorder createMatteBorder (int top, int left, int bottom,
Int right, Icon );
Public static Border createRaisedBevelBorder ();
Public static TitledBorder createTitledBorder (Border border );
Public static TitledBorder createTitledBorder (Border border, String title );
Public static TitledBorder createTitledBorder (Border border, String title,
Int justification, int position );
Public static TitledBorder createTitledBorder (Border border, String title,
Int justification, int position, Font font );
Public static TitledBorder createTitledBorder (Border border, String title,
Int justification, int position, Font font, Color color );
Public static TitledBorder createTitledBorder (String title );
} We will describe the different methods of this class in the Process of describing a specific border type. For example, to create a border with a red line, we can use the following statement and associate the border with a component.

Border lineBorder = BorderFactory. createLineBorder (Color. RED); 7.1.3 Starting with specified actborder
Before learning about a single border in the javax. swing. border package, a system border needs to be paid special attention to: AbstractBorder. As mentioned above, the AbstractBorder class is the parent class of other predefined borders.

Create AbstractBorder

AbstractBorder has a constructor:

Public condition actborder () Because condition actborder is the parent class of other standard borders, this constructor is actually automatically called for other border classes.

Test the AbstractBorder method.

The AbstractBorder class provides three methods for the Border interface.

The internal region of public Insets getBorderInsets (Component c) into actborder is zero. The getBorderInsets () method must be rewritten for each predefined subclass.

The default non-transparent attribute of public boolean isBorderOpaque () implements actborder is set to false. This means that if we need to draw a dot line similar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.