tags:

views:

49

answers:

2
+1  Q: 

Setting a button

I have 2 buttons. One is "add". The other is "cancel". In the cancel button action, I want to write that until the add method is not called, this button do nothing. How can I do that?

+4  A: 

Disable the cancel button initially. Then, in the add action listener, enable the cancel button.

JButton add = new JButton("Add");
JButton cancel = new JButton("Cancel");

cancel.setEnabled(false);

// Then something along these lines...
add.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        cancel.setEnabled(true);
    }
});

NOTE: I haven't used Swing in a while, so my syntax could be off...

cmptrgeekken
Thanks a lot,I get it[:-)]
Johanna
A: 

When you initialize your app, you could call setEnabled(false) on your cancel JButton. Then in your handler for your add JButton, you could call setEnabled(true) on the cancel JButton. Here is a demo from Sun's Swing Tutorial for disabling/enabling JButtons (source code available at link).

Asaph