Return to Unit Menu | | | Terms of Use  |

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.



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.


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:
Display Button Only - Image Button:


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"
Declare (name) a button variable
3. Create a
"new" button and give it a label
Attach "action" to the button ("addActionListener(this)")
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,
event will be generated and something will happen.


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.

Console window display:consoledisplay

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.


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


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.

Console window display:but2pic2


Return to Unit Menu | | | Terms of Use  |

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,
Help us keep these resources free. Thank you.