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
 
Training Index

Magercise 5:
Using Borders

[Help | Solution | API Docs | Short Course | Magercises]

In this magercise, you will explore the borders in the javax.swing.border package. You'll create a 2 row by 3 column grid of buttons to demonstrate the preexisting borders, as well as one you create. The border you create will be a dashed border, which permits the setting of the horizontal dash width and the vertical dash height. Also, you will create a JButton that shows a different border when selected than when it is not selected.

Magercise 5 Prerequisites

None.

Skeleton Code

Borders.java

Tasks

  1. Import the Swing border package.

  2. Get a reference to the internal contents area of the JFrame.

  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.

  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.

  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.

  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.

  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.

  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.

  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.

  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.

  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.

[Result.gif]

Where help exists, the task numbers above are linked to the step-by-step help page.

The following JavaTM source file represents a solution to this Magercise.

Solution

Magercise 6

Short Course

About This Short Course

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.