Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: Borders

JavaFAQ Home » Swing, AWT Go to all tips in Swing, AWT


Bookmark and Share You can add a border to any component (JComponent), including JPanel, using the component's setBorder(...) method. To create borders, use methods in the BorderFactory class. This and other border classes can be used with
import javax.swing.border.*;
A Border object describes how to draw the border, and the same Border object may be used to set the border of many components.

Borders can be grouped into these types:

  • Empty border -- Empty space around a component.
  • Line border -- Several line styles: plain, etched, or beveled.
  • Titled border -- Text on one of the other borders.
  • Compound borders -- A combination of two borders.
  • Matte border -- Images around a component.

To create an empty border

An empty border creates empty space around a component. Give the int number of pixels of empty space on each side: top, left, bottom, right.
JPanel controls = new JPanel();
controls.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));

To create a line border

There are many styles of lines that can be put around a component. Here are some of the most useful.
Border lineBdr = BorderFactory.createLineBorder(c);
Creates a line with Color c.
Border lineBdr = BorderFactory.createLineBorder(c, w);
Creates a line border int w pixels wide with Color c.
Border etchedBdr = BorderFactory.createEtchedBorder()
An etched border looks like a line carved into the background.
Border etchedBdr = BorderFactory.createEtchedBorder(h, s);
Creates an etched border with highlight Color h and shadow Color s.
Border lowerdBdr = BorderFactory.createLoweredBevelBorder();
Makes the component look like it is below the background.
Border raisedBdr = BorderFactory.createRaisedBevelBorder();
Makes the component look like it is above the background.

To create a titled border

A titled border puts a title on another kind of border.
BorderFactory.createTitledBorder(brdr, title)
Creates a titled border with the title at the top left, on top of the existing border brdr.
import javax.swing.border.*;
. . .
JPanel controls = new JPanel();
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched, "Controls");
controls.setBorder(titled);
BorderFactory.createTitledBorder(brdr, text, just, place, fnt, clr)
Use the simple form above if possible, otherwise this more complex version can be used. See You can control the placement and color of the title with the following parameters.
brdr One of the other borders (line, etched, blank, ...).
title The title string to display.
just The text justification: TitledBorder.LEFT, TitledBorder.CENTER, TitledBorder.RIGHT, TitledBorder.LEADING, TitledBorder.TRAILING, TitledBorder.DEFAULT_JUSTIFICATION (TitledBorder.LEADING)
place The title position: TitledBorder.ABOVE_TOP, TitledBorder.TOP, TitledBorder.BELOW_TOP, TitledBorder.ABOVE_BOTTOM, TitledBorder.BOTTOM, TitledBorder.BELOW_BOTTOM, TitledBorder.DEFAULT_POSITION (TitledBorder.TOP)
fnt The title Font.
clr The Color of the title.

To create a compound border

You will often get a more attractive effect if you use a compound border, a border that is created by combining two other borders.
Border inner = BorderFactory.create . . .
Border outer = BorderFactory.create . . .
Border combined = BorderFactory.createCompoundBorder(outer, inner);

p.setBorder(combined);

Border insets on graphics panel

When you add a border to a panel you are using for graphics, you need to know the amount of space the borders use on the panel. Call the panel's getInsets method and use the left, right, top, and bottom fields. For example, to draw a panel that has (or might have) a border:
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Insets ins = this.getInsets();
    int h = this.getHeight() - ins.top - ins.bottom;
    int w = this.getWidth() - ins.left - ins.right;
    g.fillRect(ins.left, ins.top, w, h);
}

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy