C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs » Java swing/awt programs

Enable and Disable button in Java swing

In the following java program, we will learn how we can Enable or Disable button? Here we use disableElementItem for enable or disable buttons and Import BorderLayout for creating effective frame.
Submitted by IncludeHelp, on December 24, 2017

Java JToolBar and JMenuBar

JToolBar allows us to manage all the elements in a group especially buttons with icons in a row or column.

JMenuBar class is used to show menu on window or in frame of JFrame. It may have several menus. We can add more menus under each other and can manage them easily.

Java BorderLayout

The BorderLayout is the default layout and it is used in JFrame and many more like JWindow, JDialog and JApplet It is used to set height, width, space. It is used to specify the areas like CENTER, EAST, NORTH, SOUTH and WEST.

Java ContentPane

ContentPane is in JFrame which can hold the components and these components fitted and positioned and call in JFrame.There are several ways to handle the content pane, but most of them fail to provide one basic requirement -- ability to set a border.


// This program will create disable or enable button using Swing.

package Swing;

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

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

public class DisableEnableInSwing extends JFrame
	// create objects used in the frame.
	public static final String ENABLE = "ENABLE";
	public static final String DISABLE = "DISABLE";

	private JToolBar toolBar = new JToolBar();
	private JMenuBar menuBar = new JMenuBar();
	private JMenu testMenu = new JMenu("Test");
	private MyAction theAction = new MyAction(this);
	// for disable the button.
	private JMenuItem disableActionItem= new JMenuItem("Disable the Action");

	public DisableEnableInSwing() 
		// add menu objects.

		// create button in menu to disable action.
		disableActionItem.addActionListener(new ActionListener() 
			// perform the disable action.
			public void actionPerformed(ActionEvent e) 
				// this works when button is enable.
				if (e.getActionCommand().equals(DISABLE))
					disableActionItem.setText("Enable the Action");
				// otherwise enable the button.
					disableActionItem.setText("Disable the Action");
		this.getContentPane().add(toolBar, BorderLayout.NORTH);
		// will set back color and size of the frame.
		this.setSize(320, 200);

	public static void main(String[] args) 
		DisableEnableInSwing t = new DisableEnableInSwing();

class MyAction extends AbstractAction
	// create JFrame object.
	JFrame f;
	boolean toggle = true;

	// function used for changing color.
	public MyAction(JFrame f)
		super("Change Color");
		this.f = f;

	// this will perform action and change color on click.
	public void actionPerformed(ActionEvent e)
		if (toggle)
			// for blue color.
			toggle = false;
			// for red color.
			toggle = true;

Output (screenshots)

enable disable buttin in swing 0

enable disable buttin in swing 1

enable disable buttin in swing 2

enable disable buttin in swing 3

enable disable buttin in swing 4

Liked this article? Do share with your friends :)