|
-
October 17th, 2011, 08:44 AM
#1
Error in ActionListener
Hi Guys,
Anyone can help me please??
I am newbie in Java and I tried simple ActionListener and I got an error "Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable item1.
I created the item1 as JTextField already but it seem it didn't detected.
Attached the code :
public DemoForm() {
super ("Entry Form");
setSize(255,80);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new FlowLayout());
JLabel LabelNumberOfCustomer = new JLabel("Number of the customer : ", JLabel.RIGHT);
add (LabelNumberOfCustomer);
setVisible(true);
LabelNumberOfCustomer.setToolTipText("This is tool tip text");
JTextField item1 = new JTextField (3) ;
add (item1);
theHandler handler = new theHandler();
item1.addActionListener(handler);
}
private class theHandler implements ActionListener {
public void actionPerformed (ActionEvent event){
String string = "";
if (event.getSource()==item1){
string = String.format("Number of Customer : %s", event.getActionCommand());
}
JOptionPane.showMessageDialog(null, string);
}
}
-
October 27th, 2011, 10:23 AM
#2
Re: Error in ActionListener
its hard to trace the error you are mentioning because your code seems incomplete
for example your method DemoForm is outside the scope of any class and then you have class thehandler that implements actionListener
basically you just have this:
Code:
DemoForm()
class handler{
actionPerformed()
}
when you should have something like this:
Code:
class form{
demoForm()
}
class handler{
actionPerformed()
}
//and a main method withing a tester class to test your form and handler Class
class testClass{
public static void main(String[] args) {
//code use for testing
}
}
-
October 27th, 2011, 10:44 AM
#3
Re: Error in ActionListener
Please post the whole class remembering to use code tags.
The error message shown is a run time error saying the code could not be compiled which suggests you are attempting to run an invalid class ie the last compile cycle failed. Are you sure you successfully compiled the class after your last changes?
BTW are you using an IDE or compiling and running from the command line.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|