lucklshot
lucklshot

Reputation: 21

JAVA. One button that can disable two other buttons and then renable them on click

So in total i have 3 buttons. The disable button I want it to disable the other 2 buttons when clicked. Once they are disabled then the disable button text will be set to enable. Now the disable button is an enable button to enable the 2 other buttons. I do not have a problem with disabling the buttons. I am having trouble with enabling them. Here is where I am having trouble.

        btnOk.addActionListener(this);
    btnCancel.addActionListener(this);
    btnDisable.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {


    // set both buttons
    btnOk.setText("ok");
    btnCancel.setText("cancel");
    btnDisable.setText("disable");


    JButton clickedButton = (JButton) e.getSource();
    if (btnDisable == clickedButton) {
        clickedButton.setText("enable");
    } else {
        clickedButton.setText("Clicked!");
    }
    if (btnDisable == clickedButton) {

        btnOk.setText("enable");
        btnOk.setVisible(false);
        btnCancel.setVisible(false);
        disable = true;}
    }
    public void actionPerformed2(ActionEvent e) {

}

Upvotes: 0

Views: 179

Answers (1)

David Díaz
David Díaz

Reputation: 119

Buttons have the method isEnabled(). You can do

if (button.isEnabled()){
   button.setEnabled(false);
} else {
   button.setEnabled(true);     

Upvotes: 1

Related Questions