datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Creating a JButton

In Swing, the JButton class allows the programmer to create a labeled, independent button in an application. Some action event will result when the button is pushed. A button is one of several action event components we will be adding to our windows.

As seen previously, we will be using a JPanel to contain our components within the JFrame. The JPanel is an invisible container and you will not see evidence of its existence on the screen. The JPanel resides within the JFrame, and as such needs to be "added" to the frame. In a similar manner, the JButtons will need to be "added" to the JPanel.

dividerdash

JButton

reddot JButton() - default button without text or image
reddot JButton(String text) - creates button with text.
reddot JButton(Icon image) - creates button with image (icon).
reddot JLabel(String text, Icon image) - creates button with text and image (icon).

A Swing button (a JButton) can display both text and/or an image (icon).

reddot JButton button = new JButton("Button A"); //display text
reddot JButton button2 = new JButton(new ImageIcon("src/dog.gif")); //display image
*Note: ImageIcon is a fixed-size picture (usually very small) used on GUI components.
Also allowable: ImageIcon icon = new ImageIcon("src/dog.gif"));
Our images will be stored in the same file location as the programming code.
butexpic
dbDog

dividerdash

These first two code examples are only "displaying" the JButtons shown above.
No event coding is generated in these examples (nothing happens when the buttons are pushed).
Check out establishing a content pane.

Display Buttons Only - Text Buttons:
 
button1code
Display Button Only - Image Button:
 
buttonpic2

dividerdash

Activate Buttons:
 

When creating interactive components, the interface ActionListener will be needed so that the event created by the button click (in this example) can be activated. A listener interface is a connection between the "listener" (the user) and the "event source" (the click of the button).

Event "types" have matching listener interfaces. We have seen the MouseListener for Mouse Events in the last unit. There are also other interfaces such as ItemListener and KeyListener. In this unit, we will be concentrating on the use of the ActionListener.

Remember that import java.awt.event.*; will be needed.

The class using the "event listener" must implement the ActionListerner by adding implements ActionListener to the class declaration. An actionPerformed() method must then be defined. This method will receive an ActionEvent argument from the GUI.

reddot Steps to Establishing an Active Button:
1. Indicate the class "implements ActionListener"
2.
Declare (name) a button variable
3. Create a
"new" button and give it a label
4.
Attach "action" to the button ("addActionListener(this)")
5.
Add button to the JPanel (in the order which they will appear)
6. Establish
action to be taken method ("actionPerformed")

reddot The getSource() method returns the source of an event.
When a series of buttons exist, this method allows the programmer to determine which button was clicked.

Remember that when a JButton is clicked,
an
event will be generated and something will happen.

dividerdash

Example: Create buttons to choose an arbitrary paint color for your new bedroom!
Pressing a button will cause a
message to appear in the Console window. Continuing to press the choice buttons will display the second choice message or repeat the first choice message.

paintdecisionpic
Console window display:consoledisplay

BA1
BA2
Note: In this example, hitting the button changes text appearing in the Console window. It is also possible for a button to cause text changes within the JFrame as we saw in the Label lesson.

Note: The "order" in which the buttons were "added" to the JPanel is significant.
Button A appears before button B because
panel.add(A); appeared before panel.add(B); in the coding.

Note: Action needs a non-static location, so the action attachments cannot be placed in main.

dividerdash

Additional Options for Appearance of Buttons:

// default appearance
JButton A = new JButton(" Choice A "); choiceAP

NOTE: The BorderFactory is a toolkit for creating borders around GUI components. There are a wide range of options.

//create a bevel edged bordered button
reddot A.setBorder(BorderFactory.createRaisedBevelBorder()); choiceAB
Some other options:
reddot LineBorder
reddot RaisedEtchedBorder
reddot LoweredEtchedBorder
reddot LoweredBevelBorder
reddot EmptyBorder

//coloring the button and text in button
reddot B.setBackground(Color.BLUE);
reddot B.setForeground(Color.YELLOW);ChoiceBblue

dividerdash

Arranging Buttons Using Coordinates: While layout managers provide a more universal arrangement of a frame, coordinates will supply alignments that are sufficient for beginning programmers. Consider the example shown above in another arrangement using coordinates.

but2pic
Console window display:but2pic2

but2code
but2code2

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.