• Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener illegal start of expression  RSS feed

 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm attempting to use an action listener to read the contents of a text
field into a string when the enter key is pressed. I am getting an
"illegal start of expression" error on the line:
"public void actionPerformed(ActionEvent e)"
at compile time. I have included the code below.

What am I doing wrong? This is probably an obvious error to the Java pros out there; but, I am still fairly new at this. Thanks.

import javax.swing.*;
import java.io.*;
import java.awt.*;

public class Jerry_Test implements ActionListener
{

public static void start()
{

JFrame parentFrame = new JFrame();
JLabel new_label = new JLabel("New part number");
JTextField new_text = new JTextField(50);
JPanel new_panel = new JPanel();
new_label.setLabelFor(new_text);
new_panel.add(new_label);

JOptionPane new_pane = new JOptionPane(new_panel);
new_pane.setWantsInput(true);

JDialog new_dialog = new_pane.createDialog(parentFrame, "Test dialog");
new_dialog.setModal(false);
new_dialog.setVisible(true);
new_dialog.toFront();

new_text.addActionListener(ActionListener);
public void actionPerformed(ActionEvent e)
{
String new_name = new_text.getText();
});

}

public static void stop()
{
}

}
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig,

Thanks for the suggestion. It makes sense but when I try it I get a
';' expected error, on the line below
"new_text.addActionListener(ActionListener)", where I add the additional
"{".

This seems to jibe with the example code I've been looking at. I can't figure out what is wrong.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to create an anonymous inner class implementing the ActionListener interface it should look something like

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!