Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   Swing

JButton

This tutorial describes the Swing JButton class.

The JButton Class

Plain JButton

JButton with Text

JButton with Image

JButton with Image and Text

Other JButton Methods

 


The JButton Class  

The JButton class descends from the JAbstractButton class from which it inherits many methods.  You can create an unlabelled button, a plain labelled button, a button that displays an image or a button that displays both a label and an image.

There are four constructors to choose from when creating a button. 

  • JButton()

creates a plain button without any text

  • JButton(String s)

creates a button with text

  • JButton(Icon m)

an image is shown on the button

  • JButton(String s, Icon m)

both an image and text is shown on the button

   


Plain JButton   

To create a plain button without text, use a line of code such as:- 

JButton aButton = new JButton();

 


JButton with Text   

To create a button with text, use a line of code such as:-       

JButton aButton = new JButton("I am a Button");

 


JButton with Image   

To create a button with an image we need to create an Icon.  We can use the ImageIcon class for this:-

ImageIcon anIcon = new ImageIcon ("snowflake.gif");

then we can use that variable that references the ImageIcon object in the argument for the button's constructor:-

JButton aButton = new JButton(anIcon);

Alternatively, if we don't need a  variable that references the ImageIcon object we could use a single line of code as follows:-     

JButton aButton = new JButton(new ImageIcon ("snowflake.gif"));

 

Don't forget to place your image file in the same folder as you class file.

 

Important: --  Using images in applets?   Then read this and instead of creating a new ImageIcon instance with the line:-

JButton aButton = new JButton(new ImageIcon ("snowflake.gif"));

Use:-

JButton aButton = new JButton(new ImageIcon  

                      (getImage(getCodeBase(),"snowflake.gif"));

You must also import an extra package:-

import java.net.*

 

 


JButton with Image and Text   

To create a button with both an image and text use a line of code such as:-     

JButton aButton = new JButton

     (" I am a nice Button", new ImageIcon ("snowflake.gif"));

If you are writing an applet, you should use this line instead:-

JButton aButton = new JButton

       (" I am a nice Button", new ImageIcon

                              (getImage(getCodeBase(),"snowflake.gif")));

and include the following package:-

import java.net.*

 

Don't forget to place your image file in the same folder as you class file.

 

~Now try the activity~

Activity 9A

1. Create an applet with four different buttons like the one shown below.

Note: You can use my image file if you wish or find a gif of your own.

 


Other JButton Methods   

Here are some methods we can use with the JButton class.  You should note that this is not a full list, there are lots of other methods. 

JButton inherits a lot of these methods from JComponent and AbstractButton.  You should check out the inherited JComponent and JAbstractButton class methods.

  • setText(String s)

Set the text displayed by the button.

  • setHorizontalTextPosition(int i)

Set the horizontal position of the button's text relative to the button's image using as the argument: LEFT, CENTER, RIGHT, LEADING, and TRAILING (the default). 

  • setVerticalTextPosition(int i)

Set the vertical position of the button's text relative to the button's image using as the argument: TOP, CENTER (the default), and BOTTOM.

  • setPressedIcon(Icon m)

Set the image displayed by the button when it's being pressed.

  • setRolloverIcon(Icon m)

  • setRolloverEnabled(boolean b)

Use setRolloverIcon(Icon m) with setRolloverEnabled(true)to make the button display the specified icon when the cursor passes over it.

 

~Now try the activity~

Activity 9B

  1. Using appropriate methods of the JButton , JAbstractButton and JComponent class, create an applet with five buttons like the one shown below.   

The first one is a plain button.  The second one is disabled.  The third one is displaying an animated gif.  The fourth one is set to change the image when the user presses it.  The fifth one is set to change the image if the user hovers the mouse cursor over it.  Finally, the first four buttons display tool tips.

~~~Applet~~~

Some of the buttons have tool tips - check them out

Note: You can use my images if you wish or find a gifs of your own. snowflake, darksnowflake, nightcloud, nightcloud2, question.

 

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002