I have three classes, one main class, one GUI class which uses awt+swing to make a basic window with 4 buttons.
//BEGIN ADD ACTION LISTENERS
handle_events event_handler=new handle_events();
home_b.addActionListener(event_handler);
my_account_b.addActionListener(event_handler);
my_history_b.addActionListener(event_handler);
exit_b.addActionListener(event_handler);
//END ADD ACTION LISTENERS
My handle_events class looks like this:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class handle_events implements ActionListener
{
public handle_events(){}
public void actionPerformed(ActionEvent e)
{
if(e.getSource==home_b) {do stuff;}
//etc
}
}
//END EVENT HANDLER CLASS
The problem is that home_b can’t be found by the ActionListener class, regardless of what I do. Thanks for all your help.
Because your handle_events class is in another scope it will never find the home_b variable. That’s the reason a lot people use Anonymous Listener classes for event handlers.
The biggest benefit to doing it this way is that you don’t need to check to see who is the source, you know it right then and there in the code what that handler needs to do.