Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

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


///////////////////////////////////////
0
misdevelopment
Asked:
misdevelopment
  • 4
  • 3
  • 2
  • +2
3 Solutions
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now