Implementing an actionlistener to a JTextField

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(); } });

How do you add an ActionListener onto a JButton in Java

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