Button Event Handling

Hi. I am currently making a program that uses 3 buttons. I am having trouble with the action listener. I am trying to do events based on which button was clicked but the getSource() comparison isn't working. The whole actionPerformed event works fine when I press any of the buttons but I am having problems with specific buttons.

The part looks something like this

JButton exit=new JButton("Exit");

exit.addActionListener(this);

publicvoid actionPerformed(ActionEvent clicked)

{

if(clicked.getSource() == exit)

{

//do exit

}

I can't seem to find what's wrong with the if statement comparison. The program also uses keyevents and they work just fine using getSource() comparison. I tried doing all sorts of typecasting and stuff but I still can't get it working.

[1135 byte] By [daRoostera] at [2007-11-26 13:10:03]
# 1
anyone have any idea what could be wrong?
daRoostera at 2007-7-7 17:23:41 > top of Java-index,Security,Event Handling...
# 2
what is your problems ?can you post your complate code ?
panji_tengkoraka at 2007-7-7 17:23:41 > top of Java-index,Security,Event Handling...
# 3

final JButton exitButton = new JButton("Exit");

exitButton.addActionListener(new ActionListener()) {

public void actionPerformed(final ActionEvent ae) {

// Do Exit Processing

}

}

Is a preferable paradigm. Using Actions would be even better.

If you really want to go the bigass-if-else route, you'll need to post

a code sample that breaks. Based on what you typed, it looks

like it should work.

es5f2000a at 2007-7-7 17:23:41 > top of Java-index,Security,Event Handling...
# 4
Hi, The getSource() method always returns, so we have to type cast it to the Button class and then compare it...........
SridharJavaa at 2007-7-7 17:23:42 > top of Java-index,Security,Event Handling...