CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Layout

Controls and layout managers
Components
Borders

A Swing border is defined in the Border interface. Every instance of JComponent can set a border through the border property defined in JComponent. If a border is present, it replaces the inset.

The AbstractBorder class implements an empty border with no size. This provides a convenient base class from which other border classes can be easily derived.

Radio buttons with an etched and titled border

How to work with borders

  • To place controls in a border, you must create a panel, create a border and apply it the panel, and add the controls to the panel.
  • Because a border only groups controls visually, you must still use a ButtonGroup object to group radio buttons logically.
  • To set borders, you must import the javax.swing.border package.

Static methods of the BorderFactory class

Method Description
createEmptyBorder() Creates an empty border that takes up no space.
createLineBorder() Creates a line border.
createLineBorder(Color color)Creates a line border with the specified color.
createLineBorder(Color color, int thickness) Creates a line border with the specified color and width.
createEtchedBorder() Creates an etched border.
createEtchedBorder(int type) Creates a border with an "etched" look using the component's current background color for highlighting and shading.
createEtchedBorder(Color highlight, Color shadow) Creates a border with an "etched" look using the specified highlighting and shading colors.
createLoweredBevelBorder() Creates a lowered bevel border.
createRaisedBevelBorder() Creates a raised bevel border.
createMatteBorder(int top, int left, int bottom, int right, Color color) Creates a matte-look border using a solid color.
createMatteBorder(int top, int left, int bottom, int right, Icon tileIcon) Creates a matte-look border that consists of multiple tiles of a specified icon.
createCompoundBorder() Creates a compound border with a null inside edge and a null outside edge.
createCompoundBorder(Border outsideBorder, Border insideBorder) Creates a compound border specifying the border objects to use for the outside and inside edges.
createTitledBorder(String) Creates a line border with the specified title.
createTitledBorder(Border,String) Adds the specified title to the specified border.

Method of the JComponent class used to set borders

Method Description
setBorder(Border) Sets the border style for a component.

Code that creates bordered radio buttons

uspsRadioButton = new JRadioButton("USPS", true);
upsRadioButton = new JRadioButton("UPS");
fedexRadioButton = new JRadioButton("Fedex");
ButtonGroup shipViaGroup = new ButtonGroup();
shipViaGroup.add(uspsRadioButton);
shipViaGroup.add(upsRadioButton);
shipViaGroup.add(fedexRadioButton);

JPanel shipViaPanel = new JPanel();
Border shipViaBorder = BorderFactory.createEtchedBorder();
shipViaBorder = BorderFactory.createTitledBorder(shipViaBorder, "Carrier");
shipViaPanel.setBorder(shipViaBorder);
shipViaPanel.add(uspsRadioButton);
shipViaPanel.add(upsRadioButton);
shipViaPanel.add(fedexRadioButton);
add(shipViaPanel);

BorderDemo will appear below in a Java enabled browser.

Previous | Summary | Text areas | Scroll panes | Check boxes | Radio buttons | Borders | Combo boxes | Event listeners | Lists | Multiple selections in a list | List models | Next