Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:00:52+00:00 2026-06-15T20:00:52+00:00

I have sample code using Swing. package playerlist; import java.awt.FlowLayout; import javax.swing.*; import java.awt.event.*;

  • 0

I have sample code using Swing.

package playerlist;

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

public class Sample extends JFrame{
    private JButton button1;
    private JButton button2;

    public Sample(){
        super();
        setTitle("Sample JFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button1 = new JButton("Button 1");
        button2 = new JButton("Button 2");

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                button1ActionPerformed(e);
            }
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                button2ActionPerformed(e);
            }
        });

        setLayout(new FlowLayout());

        add(button1);
        add(button2);
        pack();
    }

    private void button1ActionPerformed(ActionEvent ae){
        button1.setEnabled(false);
        button2.setEnabled(false);
        try{
              Thread.sleep(5000);
        }catch(Exception e){

        }
        System.out.println("*** Button 1 Clicked ***");
        button1.setEnabled(true);
        button2.setEnabled(true);
    }

    private void button2ActionPerformed(ActionEvent ae){
        button1.setEnabled(false);
        button2.setEnabled(false);
        try{
            Thread.sleep(5000);
        }catch(Exception e){

        }
        // I have disabled this button from button 1's action, but still when I click this button within
        // 5 seconds, actions of this button is performed
        System.out.println("*** Button 2 Clicked ***");
        button1.setEnabled(true);
        button2.setEnabled(true);
    }

    public static void main(String [] args){
        new Sample().setVisible(true);
    }
}

I want like – when I click button1(when button1’s action starts), button1 and button2 should be disabled(if I click on disabled button, no actions should be performed). I have disabled both buttons using setEnabled(false). And when action of button1 completes, both buttons should be enabled.
But in my code this is not working, even after disabling button, actions are being performed on disabled button.
In action of button1 I have disabled both buttons and used sleep method to pause execution (for simulating heavy work) for 5 seconds, but within 5 seconds If I click any buttons, their actions are triggered after completion of action of button1.
Please help me. I have provided sample code, when you run it, and after clicking button1, then immediately button2, actions of both buttons are performed.
I want when I press any buttons, heavy work will be done in button’s click action, and meanwhile I will disable all buttons, so no other actions can be performed. When first action completes, I will enable all buttons.
Please help me.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:00:54+00:00Added an answer on June 15, 2026 at 8:00 pm

    I got this working by running task to be performed on click of button on new thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; @SuppressWarnings(serial) public class Picture extends JFrame{
I have created a CPTTradingRangePlot using the up-to-date sample code as an example. The
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
So I have a few properties that I'm using in some sample code I'm
I am using the sample provided here and have used the code inside a
I have some sample code which is using factories. I'd like to clean up
I'm trying to create a SWING application using Java 1.6 and I have a
In trying to write more testable Java code, I have been using the Model-View-Presenter
I have a simple code using ctypes: Python 3.1.1 (r311:74480, Feb 23 2010, 11:06:41)
I have this simple code, using Joda-time. Works fine, but I have a problem.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.