Romulus3799
Romulus3799

Reputation: 1897

Enabling/disabling JButtons only works once

So I am creating an app that lets you submit question answers and then move on to the next once. I am doing this using two JButtons, "submit" and "next". Whenever I click submit, I want to disable the submit button, and enable the next button. When I click next, I want to disable the next button, and enable the submit button.

When testing my program, this relationship works once for each of the buttons, but then they don't disable each other, and both buttons remain enabled forever.

How do I make this JButton relationship work as many times as it is called? Here is the code setting up both buttons.

    buttonSubmit = new JButton(new AbstractAction("Submit"){

        public void actionPerformed(ActionEvent e){
            try {
                check((int)(engine.eval(expression)));
            } catch (ScriptException e1) {
            }

            buttonNext.setEnabled(true);
            setEnabled(false);

        }

    });

    buttonNext = new JButton(new AbstractAction("Next Question"){

        public void actionPerformed(ActionEvent e){
            setQuestionArithmetic();
            answer.setText("");
            correct.setText("");

            buttonSubmit.setEnabled(true);
            setEnabled(false);
        }
    });

    buttonNext.setEnabled(false);

Upvotes: 2

Views: 833

Answers (1)

rdonuk
rdonuk

Reputation: 3956

With this setEnabled(false); you are disabling the action, not the button. Try to disable buttons in actionPerformed methods.

buttonSubmit = new JButton(new AbstractAction("Submit"){

    public void actionPerformed(ActionEvent e){
        try {
            check((int)(engine.eval(expression)));
        } catch (ScriptException e1) {
        }

        buttonNext.setEnabled(true);
        buttonSubmit.setEnabled(false);

    }

});

buttonNext = new JButton(new AbstractAction("Next Question"){

    public void actionPerformed(ActionEvent e){
        setQuestionArithmetic();
        answer.setText("");
        correct.setText("");

        buttonSubmit.setEnabled(true);
        buttonNext.setEnabled(false);
    }
});

buttonNext.setEnabled(false);

Upvotes: 3

Related Questions