Differences between action and actionListener
What is the difference between action and actionListener, and when should I use action versus actionListener? 4 Answers 4
What is the difference between action and actionListener, and when should I use action versus actionListener? 4 Answers 4
From what you’ve provided, it looks like you’ve imported or implemented a class other than java.awt.event.ActionListener named, ActionListener (class name conflict). Try qualifying the parameter as java.awt.event.ActionListener: //Creates textField JTextField input=new JTextField(20); input.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent event) { direction=input.getText(); } });
Two ways: 1. Implement ActionListener in your class, then use jBtnSelection.addActionListener(this); Later, you’ll have to define a menthod, public void actionPerformed(ActionEvent e). However, doing this for multiple buttons can be confusing, because the actionPerformed method will have to check the source of each event (e.getSource()) to see which button it came from. 2. Use anonymous inner classes: jBtnSelection.addActionListener(new ActionListener() { public void … Read more