lol
lol

Reputation: 3390

Actions performed on jButton after disabling

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.

Upvotes: 3

Views: 1373

Answers (2)

lol
lol

Reputation: 3390

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

Upvotes: 1

mKorbel
mKorbel

Reputation: 109815

Upvotes: 3

Related Questions