JAVA - SWING - Event handlers for jTextbox - not sure what I'm doing wrong here.


Below is an excerpt from a class I have for a Jframe form. Basically, I have one text box on there, jTextbox1. I want to call an event when the enter key is pressed.
Fairly basic stuff I admit, but I'm getting problems with the line : jTextField1.addActionListener(handler);  

This returns an error of "identifier expected".

I've been looking at the screen for about 2 hours and kinda feel a bit silly asking such a basic question on here, but then I guess that's what it's all about. Hopefully I'll be able to contribute in some of the areas that I'm more proficient in!


/////////////////////////////////////////////////////////////////////

jTextField1.addActionListener(handler);import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class PlateCutterTrackerUI extends javax.swing.JFrame {
     
    /** Creates new form PlateCutterTrackerUI */
         
    TextFieldHandler handler = new TextFieldHandler();
    jTextField1.addActionListener(handler);
   
   
        private class TextFieldHandler implements ActionListener  // Private class for handling textfield events
        {
           
              public void actionPerformed(ActionEvent event)
              {
                               
                    if ( event.getSource() == jTextField1) System.out.println("test output");
                                   
               }

        } // END OF PRIVATE CLASS TextFieldHandler


///////////////////////////////////////
misdevelopmentAsked:
Who is Participating?
 
hoomanvCommented:
also this error could be occured when you have code that is not inside a method. The only statements that can appear outside methods in a Java class are declarations.
0
 
objectsCommented:
use a KeyListener

        private class TextFieldHandler extends KeyAdapter  // Private class for handling textfield events
        {
           
              public void     keyPressed(KeyEvent e)               {
                               
                    if ( event.getSource() == jTextField1) System.out.println("test output");
                                   
               }

0
 
objectsCommented:
sorry, missed a bit:

    jTextField1.addKeyListener(handler);
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
hoomanvCommented:
> identifier expected
misdevelopment your approach is correct
when u define a method, u need to specify its return type. When return type is missing, u will likely to get this error.
0
 
CEHJCommented:
>>jTextField1.addActionListener(handler);

You cannot oridnarly call methods outside methods. Do

public PlateCutterTrackerUI() {
    TextFieldHandler handler = new TextFieldHandler();
    jTextField1.addActionListener(handler);
}

although you should probably save the reference to the handler

public PlateCutterTrackerUI() {
    handler = new TextFieldHandler();
    jTextField1.addActionListener(handler);
}
0
 
misdevelopmentAuthor Commented:
Many thanks,

 public PlateCutterTrackerUI() {
                 
         
        initComponents();
       
     TextFieldHandler handler = new TextFieldHandler();
     jTextField1.addActionListener(handler);
                   
    }

Worked great :-) Although initially I foolishly put the statements prior to the init method ...oops

JC
0
 
hoomanvCommented:
the clarification of the error was mentioned by me. you've had to split the poits
0
 
misdevelopmentAuthor Commented:
I didn't see an option for splitting points, just an accept. I'll look into how to do this retrospectively. Apologies.

JC
0
 
objectsCommented:
from the EE guidleines

"If you basically agree with another comment but have something more to add, remember to give credit for the original suggestion -- mention that Expert by name -- in your post."

:)
0
 
objectsCommented:
Sorry, was just clarifying my comment.

> Your advice was to put it in the event handling code and to make the Listener a KeyListener.

I was not referring to my code (nor did I state I was)
0
 
VenabiliCommented:
And the other one had a pretty good explanation - 1 minute difference. Which in most cases mean parallel typing :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.