ActionListener / Event help
Hi all, I'm a first year comp engineering major and am having some issues with a program I am writing. Hope someone can help....
I am writing a GUI program and need to catch the user action when a certain button is clicked. I have set up all the ActionListeners and ActionEvents however I am not sure how to write the "doButtonClicked" methods for them. I need help in creating such a method for both a JButton and a JComboBox.
Here is my code, can anyone please help. Thanks in advance.
Code for JButton (need help writing -
private void doIterateButtonClicked(int numIterations) method) :
JButton iterateOne =new JButton("Iterate Once");//Iterate once button
iterateOne.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
doIterateButtonClicked(1);
}
});
Code for JComboBox
(need help writing - doThresholdChanged(JComboBox comboBox) method):
JComboBox choices =new JComboBox(THRESHOLDS);//Thresholds selection box
choices.setSelectedIndex(0);
choices.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
doThresholdChanged((JComboBox)e.getSource());
}
});
[1809 byte] By [
kumar103a] at [2007-11-27 5:49:42]

# 3
> > iterateOne.addActionListener((new ActionListener())
>
ActionListener is an interface. Interfaces cannot be instantiated. This is from the API: The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.
# 4
I'll demonstrate a small code here, you'll have to adapt it for your code
public class MainClass{
public static void main(String[] args){
JFrame frame = new JFrame("Event handling");
JButton button = new JButton("Click Me");
button.addActionListener(new MyActionListener());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}// end main()
}// end MainClass
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
doButtonClicked();
}// end actionPerformed
doButtonClicked(){
// Your code
} // end doButtonClicked
}// end class
refer docs for events and event listeners for more details