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.

Program

// 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.
		this.setJMenuBar(menuBar);
		
		menuBar.add(testMenu);
		testMenu.add(theAction);
		toolBar.add(theAction);

		// create button in menu to disable action.
		disableActionItem.setActionCommand(DISABLE);
		testMenu.add(disableActionItem);
    
		disableActionItem.addActionListener(new ActionListener() 
		{
			// perform the disable action.
			public void actionPerformed(ActionEvent e) 
			{
				// this works when button is enable.
				if (e.getActionCommand().equals(DISABLE))
				{
					theAction.setEnabled(false);
					disableActionItem.setText("Enable the Action");
					disableActionItem.setActionCommand(ENABLE);
				}
				// otherwise enable the button.
				else
				{
					theAction.setEnabled(true);
					disableActionItem.setText("Disable the Action");
					disableActionItem.setActionCommand(DISABLE);
				}
			}
		});
		
		this.getContentPane().add(toolBar, BorderLayout.NORTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// will set back color and size of the frame.
		this.getContentPane().setBackground(Color.red);
		this.setSize(320, 200);
		this.setVisible(true);
	}

	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.
			f.getContentPane().setBackground(Color.blue);
			toggle = false;
		}
		else
		{
			// for red color.
			f.getContentPane().setBackground(Color.red);
			toggle = true;
		}
		f.repaint();
	}
}

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






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.