Header javaperspective.com

5.14. Buttons
Last updated: 25 January 2013.

This tutorial will show you how to use buttons in Java.

So far, you have seen several buttons in action in the previous tutorials. However, apart from registering event listeners, no other features provided by buttons have been used. A button is an instance of the class JButton but most of its features are provided by the super class AbstractButton. You can bind an icon, a tooltip text and a keyboard shortcut to a button. You can also unable/disable a button and set the default button within a container.

The picture shown below is a frame containing several buttons. Each button is described by a tooltip text that appears when the mouse hovers the button.


Buttons



Here is the code:


import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public final class Buttons extends JFrame implements ActionListener{

   
private JButton button1;
   
private JButton button2;
   
private JButton button3;
   
private JButton button4;
   
private JButton button5;
   
private JButton button6;

   
public Buttons(){
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
public void actionPerformed(ActionEvent e){
         
JButton source = (JButton) e.getSource();
          System.out.println
(source.getText() + " has been clicked");

         
if(source.equals(button5))
               
button1.setEnabled(false);
         
else if(source.equals(button6))
               
button1.setEnabled(true);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

          ImageIcon buttonIcon =
new ImageIcon("../images/button-icon.gif");
          Dimension buttonSize =
new Dimension(100, 40);

          button1 =
new JButton("Button 1");
          button1.setMnemonic
(KeyEvent.VK_1);
          button1.addActionListener
(this);
          setSizeAndAlignment
(button1, buttonSize, Component.CENTER_ALIGNMENT);
          button1.setToolTipText
("A button whose keyboard shortcut is 1");

          button2 =
new JButton(buttonIcon);
          button2.setMnemonic
(KeyEvent.VK_2);
          button2.addActionListener
(this);
          setSizeAndAlignment
(button2, buttonSize, Component.CENTER_ALIGNMENT);
          button2.setToolTipText
("A button whose keyboard shortcut is 2. It has an icon but no text");

          button3 =
new JButton("Button 3", buttonIcon);
          button3.setMnemonic
(KeyEvent.VK_3);
          button3.addActionListener
(this);
          button3.setVerticalTextPosition
(AbstractButton.CENTER);
          button3.setHorizontalTextPosition
(AbstractButton.LEADING);
          setSizeAndAlignment
(button3, buttonSize, Component.CENTER_ALIGNMENT);
          button3.setToolTipText
("A button whose keyboard shortcut is 3. It has text and an icon on the right hand side of the text.");

          button4 =
new JButton("Button 4", buttonIcon);
          button4.setMnemonic
(KeyEvent.VK_4);
          button4.addActionListener
(this);
          button4.setVerticalTextPosition
(AbstractButton.CENTER);
          button4.setHorizontalTextPosition
(AbstractButton.TRAILING);
          setSizeAndAlignment
(button4, buttonSize, Component.CENTER_ALIGNMENT);
          button4.setToolTipText
("A button whose keyboard shortcut is 4. It has text and an icon on the left hand side of the text.");

          button5 =
new JButton("Button 5", buttonIcon);
          button5.setMnemonic
(KeyEvent.VK_5);
          button5.addActionListener
(this);
          button5.setVerticalTextPosition
(AbstractButton.BOTTOM);
          button5.setHorizontalTextPosition
(AbstractButton.CENTER);
          setSizeAndAlignment
(button5, buttonSize, Component.CENTER_ALIGNMENT);
          button5.setToolTipText
("A button whose keyboard shortcut is 5. It has text and an icon on top of the text. This button disables the button 1. This button is the default button (selected at startup).");

          button6 =
new JButton("Button 6", buttonIcon);
          button6.setMnemonic
(KeyEvent.VK_6);
          button6.addActionListener
(this);
          button6.setVerticalTextPosition
(AbstractButton.TOP);
          button6.setHorizontalTextPosition
(AbstractButton.CENTER);
          setSizeAndAlignment
(button6, buttonSize, Component.CENTER_ALIGNMENT);
          button6.setToolTipText
("A button whose keyboard shortcut is 6. It has text and an icon at the bottom of the text. This button enables the button 1");

          contentPane.add
(Box.createVerticalGlue());
          contentPane.add
(button1);
          contentPane.add
(Box.createVerticalGlue());
          contentPane.add
(button2);
          contentPane.add
(Box.createVerticalGlue());
          contentPane.add
(button3);
          contentPane.add
(Box.createVerticalGlue());
          contentPane.add
(button4);
          contentPane.add
(Box.createVerticalGlue());
          contentPane.add
(button5);
          contentPane.add
(Box.createVerticalGlue());
          contentPane.add
(button6);
          contentPane.add
(Box.createVerticalGlue());

         
// Make the button 5 the default button.
         
getRootPane().setDefaultButton(button5);

          add
(contentPane);
   
}


   
private void init(){
         
setTitle("Buttons");
          setSize
(225, 350);
          setLocationRelativeTo
(null);
   
}


   
private void setSizeAndAlignment(JComponent component, Dimension size, float alignment){
         
component.setPreferredSize(size);
          component.setMaximumSize
(size);
          component.setMinimumSize
(size);

          component.setAlignmentX
(alignment);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new Buttons();
               
}
          })
;
   
}
}

The method setMnemonic(int mnemonic) sets the keyboard shortcut of a button. Thus, the combination of the Alt key with the specified value triggers the button's click.

As you can see, the frame's default button is set to the button 5. As a result, the button 5 is selected by default at startup. Moreover, right after startup, the default button is clicked when the user hits enter, provided that the frame has the focus.

As I said earlier, the class JButton inherits AbstractButton which contains most of the features provided by buttons. The class AbstractButton is the base class that defines features commonly shared by several other components like toggle buttons, check boxes, radio buttons and menus. The next tutorials will show you how to use those components.


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.14. Buttons
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.15. Toggle buttons

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7