Borders

A Border can be placed on any Component. The example below shows some basic Borders.

BorderDemo example: (Run Applet)

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class BorderDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ListSelectionListener
    {
        private final ImageIcon smiley = new ImageIcon(getClass().getClassLoader().getResource("images/smiley.jpg"));
        private final Border borders[] =
        {
            BorderFactory.createEmptyBorder(),
            BorderFactory.createEtchedBorder(),
            BorderFactory.createLineBorder(Color.red),
            BorderFactory.createLoweredBevelBorder(),
            BorderFactory.createMatteBorder(4, 4, 2, 2, Color.gray), // top, left, bottom, right
            BorderFactory.createRaisedBevelBorder(),
            BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.green), "Title", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION),
            BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder()),
            BorderFactory.createMatteBorder(10, 10, 10, 10, this.smiley)
        };
        private final String borderNames[] =
        {
            "Empty", "Etched", "Line", "Lowered Bevel", "Matt", "Raised Bevel", "Titled", "Compound", "icon"
        };
        private final JList borderList = new JList(this.borderNames);

        public View()
        {
            super();
            this.borderList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            this.add(this.borderList);
            this.borderList.addListSelectionListener(this);
        }

        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            this.borderList.setBorder(this.borders[this.borderList.getSelectedIndex()]);
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>