Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 

Help is available for each task, or you can go straight to the solution source code.

Task 1

Import the Swing border package.
import javax.swing.border.*;

Task 2

Get a reference to the internal contents area of the JFrame.
Container content = getContentPane();

Task 3

Create a 2 row by 3 column grid to layout the buttons in. Leave a 5 pixel horizontal and vertical gap, to better show where the various buttons begin and end.

Add the following method call to the constructor.

content.setLayout (new GridLayout (2, 3, 5, 5));

Task 4

For the first button, create a JButton and set its border to a gray LineBorder. Have the button's label be "One," and add it to the display.

You can either pass the LineBorder constructor the color gray or use the createGrayLineBorder() method.

b = new JButton("One");
b.setBorder (LineBorder.createGrayLineBorder());
content.add (b);

Task 5

For the second button, create a JButton and set its border to be a EtchedBorder. Have the button's label be "Two," and add it to the display.
b = new JButton("Two");
b.setBorder(new EtchedBorder());
content.add (b);

Task 6

For the third button, create a JButton and set its border to be empty, with two pixels of space all around. EmptyBorder is the empty border. Have the button's label be "Three," and add it to the display.
b = new JButton("Three");
b.setBorder(new EmptyBorder(2,2,2,2));
content.add (b);

Task 7

For button four, use a TitledBorder to display a title of "Press Me" in a blue, 10-point italic Serif font, centered on the top of a black LineBorder. Have the button's label be "Four," and add it to the display next.
b = new JButton("Four");
b.setBorder(new TitledBorder(
  LineBorder.createBlackLineBorder(),
  "Press Me",
  TitledBorder.CENTER, 
  TitledBorder.TOP,
  new Font ("Serif", Font.ITALIC, 10),
  Color.blue));
content.add (b);

Task 8

Before creating the fifth button, create a subclass of JButton that will show a different border when the button is selected (pressed-in). Call the class DoubleBorderedButton and have its constructor accept two Border objects as parameters. Add a MouseListener and have it toggle between the borders when the mouse button is pressed or not.
class DoubleBorderedButton extends JButton {
  public DoubleBorderedButton (final Border normal,
                             final Border selected) {
    setBorder (normal);
    addMouseListener (new MouseAdapter() {
      public void mousePressed (MouseEvent e) {
        setBorder (selected);
      }
      public void mouseReleased (MouseEvent e) {
        setBorder (normal);
      }
    });
  }
}

Task 9

Now, for the fifth button, create a DoubleBorderedButton. Have one of the borders be a raised BevelBorder with a blue highlight color and a yellow shadow. For the other, have the border be a lowered BevelBorder with the same colors. Have the button's label be "Five," and add it to the display.
b = new DoubleBorderedButton(
      new BevelBorder(BevelBorder.RAISED,
        Color.blue, Color.yellow),
      new BevelBorder(BevelBorder.LOWERED,
        Color.blue, Color.yellow));
b.setText ("Five");
content.add (b);

Task 10

The final button uses a new border, DashedBorder that shows a dashed border line, instead of the solid one used with LineBorder. Before you can use it though, you need to complete the definition. The Border interface has three methods, paintBorder(), isBorderOpaque(), and getBorderInsets(). The paintBorder() and isBorderOpaque() methods are already defined. Define the getBorderInsets() method to return an Insets object that is THICKNESS wide/high all around. THICKNESS is a constant defined within the DashedBorder class.
public Insets getBorderInsets(Component c) {
  return new Insets (THICKNESS, THICKNESS, 
                     THICKNESS, THICKNESS);
}

Task 11

Since the code to add the sixth button is already present, you only need to save everything and compile the program. Then run it to see the results. Be sure to select the fifth and sixth buttons to show the selected border.

As a precaution, the Save command appends a "1" to the end of the filename you want to save. This can help prevent you from accidently overwriting your source code.

javac Borders.java
java Borders

If you are using Swing with JDK 1.1.+, make sure your CLASSPATH environment variable has the swingall.jar file in it.

Return to Magercise 5

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 19-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.