JButton Border Example


JButton Border Example Note: It is advised to read Java JFC Swing Introduction and Swing Overview before reading this.

In the following program on JButton Border Example, special features like mnemonic(shortcut key), setting borders and images are discussed.

import javax.swing.*;  		
import java.awt.*;  		
import java.awt.event.* ;  	

public class JButtonDemo3 extends JFrame implements ActionListener
{
  JButton rb, gb, bb ;
  Container c;
  public JButtonDemo3()
  {
    c = getContentPane();
    c.setLayout(new FlowLayout()) ;
                            // create images of icon size to keep on the button
    ImageIcon ic1 = new ImageIcon("bird1.gif");
    ImageIcon ic2 = new ImageIcon("Tomcat.gif");
   	       			     // button with all the properties
    rb = new JButton("Red", ic1);           // create a JButton and place a label along with the icon
    rb.setRolloverIcon(ic2) ;      // when the button attracts the focus of the mouse, icon changes from bird1.gif to tomcat.gif
    rb.setMnemonic('R');                                                                 
                                        // keyboard shortcut, press alt + R
    rb.addActionListener(this); // registering rb with ActionListener
    c.add(rb);

    gb = new JButton("Green"); // a button without icon
    gb.addActionListener(this);
    c.add(gb);

    bb = new JButton("My Beautiful Blue"); 
                            // we can add a border also to the button
    bb.setBorder(BorderFactory.createTitledBorder("Blue Button"));

    bb.addActionListener(this);
    c.add(bb);
                             // following statement closes the window
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
	System.exit(0);
      }
    } ) ;

    setTitle("Learning JButton");
    setSize(400,300);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
    String str = e.getActionCommand( );  // returns the label of the button
    JButton btn = (JButton) e.getSource( );  // returns the object of the button
    if(btn == rb )
      c.setBackground(Color.red);
    else if(btn == gb )
      c.setBackground(Color.green);
    else if(btn == bb )
      c.setBackground(Color.blue);

    JOptionPane.showMessageDialog(null, "You clicked " + str + " Button", "Learning JButton", JOptionPane.INFORMATION_MESSAGE);
  }
  public static void main( String args[])
  {
    new JButtonDemo3();
  }
}


JButton Border Example

ImageIcon ic1 = new ImageIcon(“bird1.gif”);

Output screenshot on JButton Border Example

ImageIcon is a class from Javax.swing package which creates an image of icon size to be used along with components. Any image of any format (like .gif,.jpg, .png) etc. is to be converted into an object of ImageIcon before added to any swing component.

rb = new JButton(“Red”, ic1);

The above statement creates a button object rb with label "Red" and keeps an icon(ic1 that represents bird1.gif) on the button .

rb.setRolloverIcon(ic2);

setRolloverIcon(ic2) is an attractive feature for a button. When the mouse cursor is taken on to the button, the default icon on the button ic1(of bird1) is changed to ic2(of tomcat). When the cursor is again moved away from the button, the default icon bird1 is displayed. That is, the image on the button is toggled between ic1 and ic2.

rb.setMnemonic(‘R’);

setMnemonic(char ch) method sets a keyboard shortcut to the button. That is, an event can be generated either by the usual way of clicking a mouse on the button or by pressing alt + ch.

Another property we can set to the button(which we have not set in this program) is tool tip which can be set as follows:

rb.setToolTipText(“click changes to red”);

String str = e.getActionCommand( );

getActionCommand() method of ActionEvent returns the label of the button clicked(in case of text field, it is the text entered).

JButton btn = (JButton) e.getSource();

getSource() method of the ActionEvent class returns the object of the button in an object form of Object class which must be type casted to JButton. This, we have done earlier in case of JTextField.

JFrame is closed using anonymous inner class of WindowAdapter.

bb.setBorder(BorderFactory.createTitledBorder(“Blue Button”));

One of the strengths of Swing is giving choicest border around a component. In the above statement, around the JButton bb, a border with a title "Blue Button" is given.

Other borders that can be applied are:

BorderFactory: createTitledBorder, createEtchedBorder, createBevelBorder, createRaisedBevelBorder, createLoweredBevelBorder, createLineBorder, createMatteBorder, createCompoundBorder, and createEmptyBorder

2 thoughts on “JButton Border Example”

Leave a Comment

Your email address will not be published.