pwnster1357
pwnster1357

Reputation: 35

How to use actionPerformed() to check to see if a button has been clicked?

public void actionPerformed(ActionEvent e) {
    if (text1.equals(e.isSelected())) {
        System.out.println("test");
    }
    else {
        System.out.println("error");
        }
    }
}

text1 is my JButton for some reason, isSelected is not working, anyone know why? any help is appreciated!

thanks in advance,

-kameron

Upvotes: 0

Views: 1436

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347214

This question raises more questions.

By it's nature, JButton does not support the property isSelected. isSelected is used by toggle style buttons, like JRadioButton, JCheckBox and JToggleButton.

By the very nature of your question, if the JButton is clicked, it will be "selected"

public void actionPerformed(ActionEvent e) {
    System.out.println("text1 has being selected");
}

Upvotes: 1

Reimeus
Reimeus

Reputation: 159784

isSelected is undefined for ActionEvent. If the ActionListener is the only one registered with the JButton, then the code will be simply:

text1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {

       System.out.println("test");
    }
});

Upvotes: 1

Related Questions