Header javaperspective.com

5.7. Borders
Last updated: 25 January 2013.

This tutorial will show you how to use borders in Java.

Borders are generally used to enhance the appearance of components' edges and, to a lower extent, to put empty space between the edges of different components. This tutorial will show you how to use the standard borders provided by Swing.

To set a component's border, use the method setBorder(Border border) provided by the class JComponent. The argument of the method setBorder(Border border) is an object that implements the interface Border. However, there is usually no need to explicitly create such an object. Instead, you can use the class BorderFactory which provides static methods that return objects that implement the interface Border.

There are 4 types of borders: simple, titled, matte and compound. The following sections show how to use each type of border.


5.7.1. Simple borders

The following picture shows several simple borders that are set around JPanel instances. In fact, although you can set borders around any subclass of the class JComponent, it is recommended to set borders around JPanel instances containing components rather than setting them directly on components because certain look and feels may behave unpredictably when borders are set around JComponent subclasses other than JPanel and JLabel.

Simple borders


Here is the code:

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;

public final class BordersFrame extends JFrame {

   
public BordersFrame() {
         
setTitle("Borders");
          setSize
(400, 350);
          setLocationRelativeTo
(null);

          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

          Dimension panelSize =
new Dimension(230, 40);
          Dimension fillerSize =
new Dimension(0, 10);

          JPanel loweredPanel =
new JPanel();
          loweredPanel.setBorder
(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
          loweredPanel.setMaximumSize
(panelSize);
          loweredPanel.add
(new JLabel("Lowered bevel border"));

          JPanel raisedPanel =
new JPanel();
          raisedPanel.setBorder
(BorderFactory.createBevelBorder(BevelBorder.RAISED));
          raisedPanel.setMaximumSize
(panelSize);
          raisedPanel.add
(new JLabel("Raised bevel border"));

          JPanel emptyPanel =
new JPanel();
          emptyPanel.setBorder
(BorderFactory.createEmptyBorder());
          emptyPanel.setMaximumSize
(panelSize);
          emptyPanel.add
(new JLabel("Empty border"));

          JPanel loweredEtchedPanel =
new JPanel();
          loweredEtchedPanel.setBorder
(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
          loweredEtchedPanel.setMaximumSize
(panelSize);
          loweredEtchedPanel.add
(new JLabel("Lowered etched border"));

          JPanel raisedEtchedPanel =
new JPanel();
          raisedEtchedPanel.setBorder
(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
          raisedEtchedPanel.setMaximumSize
(panelSize);
          raisedEtchedPanel.add
(new JLabel("Raised etched border"));

          JPanel linePanel =
new JPanel();
          linePanel.setBorder
(BorderFactory.createLineBorder(Color.GRAY));
          linePanel.setMaximumSize
(panelSize);
          linePanel.add
(new JLabel("Line border"));

          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(loweredPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(raisedPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(emptyPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(loweredEtchedPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(raisedEtchedPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(linePanel);

          add
(contentPane);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new BordersFrame();
               
}
          })
;
   
}
}


5.7.2. Titled borders

A titled border is a border that displays a title describing the content of a certain area. Several titled borders are shown in the picture below:

Titled borders


The code follows:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;


public final class BordersFrame2 extends JFrame {

   
public BordersFrame2() {
         
setTitle("Borders");
          setSize
(400, 350);
          setLocationRelativeTo
(null);

          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

          Dimension panelSize =
new Dimension(270, 60);
          Dimension fillerSize =
new Dimension(0, 15);

          JPanel titledPanel =
new JPanel();
          titledPanel.setBorder
(BorderFactory.createTitledBorder("Title"));
          titledPanel.setMaximumSize
(panelSize);
          titledPanel.add
(new JLabel("Default titled border"));

          JPanel titledRaisedPanel =
new JPanel();
          titledRaisedPanel.setBorder
(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED), "Title"));
          titledRaisedPanel.setMaximumSize
(panelSize);
          titledRaisedPanel.add
(new JLabel("Titled raised border"));

          JPanel titledLoweredEtchedPanel =
new JPanel();
          titledLoweredEtchedPanel.setBorder
(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Title", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION));
          titledLoweredEtchedPanel.setMaximumSize
(panelSize);
          titledLoweredEtchedPanel.add
(new JLabel("Titled centered lowered etched border"));

          JPanel titledRaisedEtchedPanel =
new JPanel();
          titledRaisedEtchedPanel.setBorder
(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Title", TitledBorder.TRAILING, TitledBorder.DEFAULT_POSITION, new Font("Times New Roman", Font.PLAIN, 15), Color.BLUE));
          titledRaisedEtchedPanel.setMaximumSize
(panelSize);
          titledRaisedEtchedPanel.add
(new JLabel("Titled raised trailing etched border"));

          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(titledPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(titledRaisedPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(titledLoweredEtchedPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(titledRaisedEtchedPanel);

          add
(contentPane);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new BordersFrame2();
               
}
          })
;
   
}
}


5.7.3. Matte borders

A matte border is a border whose sides can be made up of a replicated icon or plain color. In the latter case, you can specify each side's width. The next picture shows several matte borders:

Matte borders


Here is the code:

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public final class BordersFrame3 extends JFrame {

   
public BordersFrame3() {
         
setTitle("Borders");
          setSize
(400, 350);
          setLocationRelativeTo
(null);

          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

          Dimension panelSize =
new Dimension(270, 60);
          Dimension fillerSize =
new Dimension(0, 15);

          JPanel matteIconTopBottomPanel =
new JPanel();
          ImageIcon icon =
new ImageIcon("../images/border-icon-1.gif", "Border icon 1");
          matteIconTopBottomPanel.setBorder
(BorderFactory.createMatteBorder(10, 0, 10, 0, icon));
          matteIconTopBottomPanel.setMaximumSize
(panelSize);
          matteIconTopBottomPanel.add
(new JLabel("Matte icon-1 top bottom border"));

          JPanel matteIconPanel =
new JPanel();
          ImageIcon icon2 =
new ImageIcon("../images/border-icon-2.gif", "Border icon 2");
          matteIconPanel.setBorder
(BorderFactory.createMatteBorder(-1, -1, -1, -1, icon2));
          matteIconPanel.setMaximumSize
(panelSize);
          matteIconPanel.add
(new JLabel("Matte icon-2 all sides border"));

          JPanel matteBluePanel =
new JPanel();
          matteBluePanel.setBorder
(BorderFactory.createMatteBorder(3, 7, 3, 7, Color.BLUE));
          matteBluePanel.setMaximumSize
(panelSize);
          matteBluePanel.add
(new JLabel("Matte blue border"));


          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(matteIconTopBottomPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(matteIconPanel);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(matteBluePanel);

          add
(contentPane);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new BordersFrame3();
               
}
          })
;
   
}
}


5.7.4. Compound borders

A compound border is the combination of two borders. The two borders themselves can be compound borders. Here are several examples:

Compound borders


The code below makes the compound borders:

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;


public final class BordersFrame4 extends JFrame {

   
public BordersFrame4() {
         
setTitle("Borders");
          setSize
(400, 350);
          setLocationRelativeTo
(null);

          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

          Dimension panelSize =
new Dimension(270, 60);
          Dimension fillerSize =
new Dimension(0, 15);

          JPanel compoundPanel1 =
new JPanel();
          Border loweredBevelBorder = BorderFactory.createBevelBorder
(BevelBorder.LOWERED);
          Border lineBorder = BorderFactory.createLineBorder
(Color.GRAY);
          compoundPanel1.setBorder
(BorderFactory.createCompoundBorder(lineBorder, loweredBevelBorder));
          compoundPanel1.setMaximumSize
(panelSize);
          compoundPanel1.add
(new JLabel("Compound line and lowered bevel"));

          JPanel compoundPanel2 =
new JPanel();
          Border raisedBorder = BorderFactory.createBevelBorder
(BevelBorder.RAISED);
          lineBorder = BorderFactory.createLineBorder
(Color.GRAY);
          compoundPanel2.setBorder
(BorderFactory.createCompoundBorder(raisedBorder, lineBorder));
          compoundPanel2.setMaximumSize
(panelSize);
          compoundPanel2.add
(new JLabel("Compound line and raised bevel"));

          JPanel compoundPanel3 =
new JPanel();
          compoundPanel3.setBorder
(BorderFactory.createCompoundBorder(loweredBevelBorder, loweredBevelBorder));
          compoundPanel3.setMaximumSize
(panelSize);
          compoundPanel3.add
(new JLabel("Compound 2 lowered bevels"));

          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(compoundPanel1);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(compoundPanel2);
          contentPane.add
(new Box.Filler(fillerSize, fillerSize, fillerSize));
          contentPane.add
(compoundPanel3);

          add
(contentPane);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new BordersFrame4();
               
}
          })
;
   
}
}


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.7. Borders
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.8. Scroll panes

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7