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...
 

Swing Chapter 3. (The basics) Frames, Panels, and Borders. Easy for reading, Click here!

Custom Search
Swing Chapter 3. (The basics) Frames, Panels, and Borders. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 2/3 



Previous Page Previous Page (1/3) - Next Page (3/3) Next Page
Subpages: 1. Frames and Panels overview
2. Borders
3. Creating a custom border

3.2    Borders

package javax.swing.border

The border package provides us with the following border classes which can be applied to any Swing component:

  BevelBorder

A 3D border with a raised or lowered appearance.

  CompoundBorder

A combination of two borders: an inside border and an outside border.

  EmptyBorder

A transparent border used to define empty space (often referred to as white space) around a component.

  EtchedBorder

A border with an etched lineappearance.

  LineBorder

A flat border with a specified thickness and color.

  MatteBorder

A border consisting of either a flat color or tiled image.

  SoftBevelBorder

A 3D border with a raised or lowered appearance, and rounded edges.

  TitledBorder

A border allowing a String title in a specific location and position. We can set the title font, color, justification, and position of the title text using TitleBorder methods and constants where necessary (see API docs).

To set the border of  a Swing component we simply call JComponent's setBorder() method. There is also a convenience class called BorderFactory, contained in the javax.swing package (not the javax.swing.border package as you might think), which contains a group of static methods used for constructing borders quickly. For example, to create an EtchedBorder we can use BorderFactory as follows:

   myComponent.setBorder(BorderFactory.createEtchedBorder());

The border classes do not provide methods set their dimensions, colors, etc. Insetad of modifying an existing border we are normally expected to create a new instance to replace the old one.

Figure 3.7 Simple Borders demonstration

<<file figure3-7.gif>>

The following code creates a JFrame containing twelve JPanels using borders of all types. Figure 3.7 illustrates:

The Code: BorderTest.java

see \Chapter3\1

import java.awt.*;

import javax.swing.*;

import javax.swing.border.*;

class BorderTest extends JFrame

{

  public BorderTest() {

    setTitle("Border Test");

    setSize(450, 450);

    JPanel content = (JPanel) getContentPane();

    content.setLayout(new GridLayout(6,2));

    JPanel p = new JPanel();

    p.setBorder(new BevelBorder (BevelBorder.RAISED));

    p.add(new JLabel("RAISED BevelBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new BevelBorder (BevelBorder.LOWERED));

    p.add(new JLabel("LOWERED BevelBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new LineBorder (Color.black, 5));

    p.add(new JLabel("Black LineBorder, thickness = 5"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new EmptyBorder (10,10,10,10));

    p.add(new JLabel("EmptyBorder with thickness of 10"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new EtchedBorder (EtchedBorder.RAISED));

    p.add(new JLabel("RAISED EtchedBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new EtchedBorder (EtchedBorder.LOWERED));

    p.add(new JLabel("LOWERED EtchedBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new SoftBevelBorder (SoftBevelBorder.RAISED));

    p.add(new JLabel("RAISED SoftBevelBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new SoftBevelBorder (SoftBevelBorder.LOWERED));

    p.add(new JLabel("LOWERED SoftBevelBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new MatteBorder (new ImageIcon("spiral.gif")));

    p.add(new JLabel("MatteBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new TitledBorder (

      new MatteBorder (new ImageIcon("spiral.gif")),

      "Title String"));

    p.add(new JLabel("TitledBorder using MatteBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new TitledBorder (

      new LineBorder (Color.black, 5),

      "Title String"));

    p.add(new JLabel("TitledBorder using LineBorder"));

    content.add(p);

    p = new JPanel();

    p.setBorder(new TitledBorder (

      new EmptyBorder (10,10,10,10),

      "Title String"));

    p.add(new JLabel("TitledBorder using EmptyBorder"));

    content.add(p);

    setVisible(true);

  }

  public static void main(String args[]) {

    new BorderTest();

  }

}

UI Guideline:

Borders for Visual Layering. Use borders to create a visual association between components on a view. Bevelled borders are graphically very strong and can be used to strongly associate items. Windows Look & Feel does this. For example buttons use a RAISED BevelBorder and data fields use a LOWERED BevelBorder. If you want to visually associate components or draw attention to a component then you can create a visual layer by careful use of BevelBorder. If you want to draw attention to a particular button or group of buttons, you might consider thickening the RAISED bevel using BorderInsets discussed in 3.2.2

Borders for Visual Grouping. Use borders to create Group Boxes. EtchedBorder and LineBorder are particularly effective for this, as they are graphically weaker then BevelBorder. EmptyBorder is also very useful for grouping. It uses the power of negative (white) space to visually associate the contained components and draw the viewer eye to the group.

You may wish to create a visual grouping of attributes or simply signify the bounds of a set of choices. Grouping related Radio Buttons and Checkboxes is particularly useful.

Achieving Visual Integration and Balance using Negative Space. Use a compound border including an EmptyBorder to increase the Negative (white) Space around a Component or Panel. Visually, a Border sets what is known as a Ground (or area) for a Figure. The Figure is what is contained within the Border. It is important to keep Figure and Ground in balance. This is done by providing adequate white space around the Figure. The stronger the border, the more white space will be required e.g. a BevelBorder will require more white space than an EtchedBorder. Ref. Mullet 95 (see Appendix B).

Border for Visual Grouping with Layering. Doubly compounded borders can be used to group information and communicate hierarchy using Visual Layering. Consider the following implementation (fig 3-8). Here we are indicating a common belonging for the attributes within the border. They are both attributes of Customer. Because we have indicated the label Customer (top LHS) in the border title, we do not need to repeat the label for each field. We are further communicating the type of the Customer with the VIP label (bottom RHS).

Visual Layering of the hierachy involved is achieved by position and font.

(i) Position: In western cultures, the eye is trained to scan from top left to bottom right. Thus something located top left has a visual higher rank than something located bottom right.

(ii) Font: By bolding the Customer, we are clearly communicating it as the highest ranking detail.
What we are displaying is a Customer of type VIP. Not a VIP of type Customer. The positioning and re-inforcing with heavier font, clearly communicate this message

Figure 3.8 Visual Grouping with Layering

<<file figure3-8.gif>>

3.2.2    Inside Borders

It is important to understand that borders are not components. In fact AbstractBorder, the abstract class all border classes are derived from, directly extends Object. Thus we cannot attach action and mouse listeners, set tool tips, etc.

Note: This has certain side effects, one of which is that borders are much less efficient in painting themselves. There is no optimization support like there is in JComponent. We can do intersting things like use a very thick MatteBorder to tile a panel with an image, but this is an inefficient (and also unreliable) solution. In general don't use really large borders for anything. If you need an extremely large border consider simulating one using JLabels and a container managed by BorderLayout.

One major benefit of Borders not being components is that we can use a single Border instance with an arbitrary number of components. In large scale apps this can reduce a significant amount of overhead.

When a Swing component is assigned a border its Insets are defined by that border's width and height settings. When layout managers lay out JComponents, as we will see in the next chapter, they take into account their Insets and normally use JComponent's getInsets() method to obtain this information. Inside the getInsets() method, the current border is asked to provide its Insets using the getBorderInsets() method.

The Insets class consists of four publicly accessible int values: bottom, left, right, top. TitleBorder must compute its Insets based on its current font and text position which will not effect every side and requires handling for many different cases--without a doubt this is the most complex border provided by Swing. In the case of CompoundBorder, both its outer and inner Insets are retrieved through calls to getBorderInsets() and then summed. A MatteBorder's Insets are determined by the width and height of its image. BevelBorder and  EtchedBorder, have Insets values: 2, 2, 2, 2. SoftBevelBorder has Insets values: 3, 3, 3, 3. EmptyBorder's Insets are simply the values that were passed in to the constructor. Each of LineBorder's Insets values equals the thickness that was specified in the constructor (or 1 as the default).

Borders get painted last in the JComponent rendering pipeline to ensure that they always appear on top of their associated component. AbstractBorder defines methods to get a Rectangle representing the interior region of the component a border is attached to: getInteriorRectangle(). Any JComponent subclass implementing its own painting methods may be interested in this area. Combined with the Graphics clipping area, components may use this information to minimize their rendering work (refer back to chapter 2).



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


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