Java JButton Tutorial with ActionListener Programming Examples

In this tutorial you will learn:

1. How to use JButton in Swing Application?
2. How to use ActionListener in JButton?
3. How to track Button click?

Java JButton in Swing

Java JButton in swing is used for performing action when clicked on it. This action might be getting data from input field, saving or retrieving data to database or performing and displaying calculation result in swing frame. Following classes and methods are used for implementing JButton in Swing.

Constructors

Constructor Description
JButton() It creates a button with no text and icon.
JButton(String s) It creates a button with the specified text.
JButton(Icon i) It creates a button with the specified icon object.

Methods

Methods Description
void setText(String s) It is used to set specified text on button
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b) It is used to set the specified Icon on the button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void addActionListener (ActionListener a) It is used to add the action listener to this object.

Programming Example

package Test.MainJava.com;
import javax.swing.*;

public class JButton_Example
{
	public static void main(String[] args)
	{
		JFrame frame=new JFrame();  
        
		JButton b=new JButton("Click Me");//creating instance of JButton  
		b.setBounds(140,100,120, 40);//x axis, y axis, width, height  
		          
		frame.add(b);//adding button in JFrame  
		          
		frame.setSize(400,500);//400 width and 500 height  
		frame.setLayout(null);//using no layout managers  
		frame.setVisible(true);//making the frame visible  
	}
}

Output:
output

Action Listener

This was the simple JButton example that performed nothing when clicking on it. Now, I will add action listener so, this button will perform some action on clicking.

Example 1: Simple Button Action Listener Program
package Test.MainJava.com;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public FirstAppxample
{
	public static void main(String[] args)
	{
		JFrame frame=new JFrame();
		JButton b=new JButton("Click Me");//creating instance of JButton 
		b.setBounds(140,100,120, 40);//x axis, y axis, width, height  
		b.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(null, "Hello World");
			}
		});
		          
		frame.add(b);//adding button in JFrame 
		          
		frame.setSize(400,500);//400 width and 500 height  
		frame.setLayout(null);//using no layout managers  
		frame.setVisible(true);//making the frame visible  
	}
	
}

Output

Example 2: Call a Method in Action Listener
package Test.MainJava.com;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JButton_Example
{
	public static void main(String[] args)
	{
		JFrame frame=new JFrame();  
		JButton b=new JButton("Click Me");//creating instance of JButton 
		b.setBounds(140,100,120, 40);//x axis, y axis, width, height  
		b.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				showmessage();
			}
		});
		b.addActionListener(null);
		          
		frame.add(b);//adding button in JFrame 		          
		frame.setSize(400,500);//400 width and 500 height  
		frame.setLayout(null);//using no layout managers  
		frame.setVisible(true);//making the frame visible  
	}	
	
	public static void showmessage()
	{
		JOptionPane.showMessageDialog(null, "Hello World");
	}
	
}

Output

Example 3: Track Which Button is Clicked and Display Message
package Test.MainJava.com;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JButton_Example
{
	protected JButton b1, b2;
	protected JFrame frame;
	
	public void TrackButton()
	{
		b1=new JButton("Button1");
		b1.setBounds(140,100,120,40);
		b1.addActionListener(actions);
		
		b2=new JButton("Button2");
		b2.setBounds(140,150,120,40);
		b2.addActionListener(actions);
		
		frame=new JFrame();
		frame.add(b1);
		frame.add(b2);
		frame.setSize(400, 500);
		frame.setLayout(null);
		frame.setVisible(true);
	}
	private ActionListener actions = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == b1)
			{
				msg1();
			}
			else if(e.getSource() == b2)
			{
				msg2();
			}
		}
	};
	public void msg1()
	{
		JOptionPane.showMessageDialog(null, "I am Button 1");
	}
	public void msg2()
	{
		JOptionPane.showMessageDialog(null, "I am Button 2");
	}
	public static void main(String[] args)
	{
		JButton_Example jb=new JButton_Example();
		jb.TrackButton();
	}	
}

Output

Summary

In this tutorial you learn how to play with JButton in Swing Application. I have tried to explain JButton with suitable, easy and complete programming example. In the next chapter you will learn some inputs field of Swing like JTextField, JPasswordField, JTextArea and JLabel


Leave a Reply

Your email address will not be published. Required fields are marked *