Link to home
Start Free TrialLog in
Avatar of misdevelopment
misdevelopment

asked on

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


///////////////////////////////////////
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of misdevelopment
misdevelopment

ASKER

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
the clarification of the error was mentioned by me. you've had to split the poits
I didn't see an option for splitting points, just an accept. I'll look into how to do this retrospectively. Apologies.

JC
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."

:)
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)
And the other one had a pretty good explanation - 1 minute difference. Which in most cases mean parallel typing :)