How to disable javax.swing.JButton in java?

jbutton java
how to disable button in java netbeans
how to disable a button in java
setenabled java
how to make a jbutton in java
adding jbutton in java
jbutton disable after click
difference between button and jbutton in java

I have created a swings application and there is a "Start" button on the GUI. I want that whenever I clicked on that "Start" button, the start button should be disabled and the "Stop" button be enabled.

For that I have written the following code in the "ActionPeformed(...)" method of the "Start" button


But the above code is not creating the desired affect on the GUI.

Is the above code correct for what I want to do?

It's not working with "repaint()" too.


The code is very long so I can't paste all the code. I can tell, though, more about the code.

In the "ActionPeformed" method of "start" button, after calling the above two statements, I am executing a "SwingWorker" thread.

Is this thread creating any problem?

For that I have written the following code in the "ActionPeformed(...)" method of the "Start" button

You need that code to be in the actionPerformed(...) of the ActionListener registered with the Start button, not for the Start button itself.

You can add a simple ActionListener like this:

JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent ae) {

note that your startButton above will need to be final in the above example if you want to create the anonymous listener in local scope.

java, import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.​swing.Timer; public class VoteCounter extends JApplet { private int APPLET_WIDTH  Returns a string representation of this JButton. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations.

The code is very long so I can't paste all the code.

There could be any number of reasons why your code doesn't work. Maybe you declared the button variables twice so you aren't actually changing enabling/disabling the button like you think you are. Maybe you are blocking the EDT.

You need to create a SSCCE to post on the forum.

So its up to you to isolate the problem. Start with a simple frame thas two buttons and see if your code works. Once you get that working, then try starting a Thread that simply sleeps for 10 seconds to see if it still works.

Learn how the basice work first before writing a 200 line program.

Learn how to do some basic debugging, we are not mind readers. We can't guess what silly mistake you are doing based on your verbal description of the problem.

Enable and Disable button in Java swing, This page provides Java code examples for javax.swing.JButton.setEnabled. setToolTipText("Click this button to disable the middle button."); b2. How to Disable or Remove JButton Border in Java Swing Intact Abode in this video i shown how to remove the border for you jlabel, jscrollpane,jbutton,jtextfield and other components in java

This works.

public class TestButton {

public TestButton() {
    JFrame f = new JFrame();
    f.setSize(new Dimension(200,200));
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout());

    final JButton stop = new JButton("Stop");
    final JButton start = new JButton("Start");
    stop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


    start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


 * @param args
public static void main(String[] args) {
    new TestButton();



Java Code Examples javax.swing.JButton.setEnabled, This class implements accessibility support for the JButton class. Nested classes/​interfaces inherited from class javax.swing.AbstractButton · AbstractButton. Hello, i'm writing a Swing Application, therefor i'm using javax.swing.JButton. At awt i could use button.setEnabled(false) and so this certain Button is not clickable, events will not be recieved, and his Layout is modified to "disabled". But with JButtons i'm able to recieve events from them, even if they're disabled?

JButton (Java Platform SE 8 ), How to disable javax.swing.JButton in java? JButton startButton = new JButton("​Start"); startButton.addActionListener(new ActionListener() { public void  How to close JFrame on the click of a Button in Java. Set frame.dispose () on the click of a button to close JFrame. At first create a button and frame −. JFrame frame = new JFrame (); JButton button = new JButton ("Click to Close!");

javax.swing.JButton.setEnabled java code examples, import java.awt.*; import javax.swing.*; import javax.swing.text.*; import javax.​swing.event.*; public class DemoJButtonDisabled extends JApplet { JButton button;  An update to the program, i have added a "Job" button, when you click it you are granted money, rather than starting with cash. If you click the Job button you must wait ten seconds to click it again, and there is a timer that shows how much time until the next job is available.

Disable « Button « Java Swing Q&A, package my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to Disable GUI Button in Java

  • The above should work, and you shouldn't need to call repaint(), could you post more code in your logic.
  • Another possibility would be to use the start button also as stop button (depends on your needs), it's enough to save the current state and just change text.
  • I know that already and am doing the same in the correct actionPerformed
  • sorry, just trying to help out.
  • You never defined stopButton
  • Agreed, do you want this spoon fed to you? You want the help, but downvote help recieved when you weren't clear. I created and example, as below but called a SwingWorker on the button press, and it worked fine in my example. Start small with things working and build up to what you want.