Results 1 to 9 of 9
Thread: Cannot Find Symbol Error
- 07-31-2013, 06:28 PM #1
Member
- Join Date
- Jul 2013
- Posts
- 13
- Rep Power
- 0
Cannot Find Symbol Error
I keep on getting this error
error: cannot find symbol
if(e.getSource==btn1);
symbol: variable getSource
location : variable e of type ActionEvent
here's the code
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Name extends JFrame
implements ActionListener
{
JButton btn1=new JButton("OK");
JLabel lbl=new JLabel("Enter your name");
JTextField txtfield=new JTextField(20);
JCheckBox chkbox=new JCheckBox("Bold");
JRadioButton radiobtn=new JRadioButton("Red");
JList list=new JList();
JTextArea txtarea=new JTextArea();
public Name()
{
setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
add(btn1);
add(lbl);
add(txtfield);
add(chkbox);
add(radiobtn);
add(list);
add(txtarea);
btn1.addActionListener(this);
//txtfield.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource == btn1)
{
String nameinput=txtfield.getText();
txtarea.setText(nameinput);
}
}
public static void main(String[] args)
{
Name obj=new Name();
obj.setTitle("LAB 3 Q2");
obj.setVisible(true);
obj.pack();
}
}
- 07-31-2013, 06:49 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Cannot Find Symbol Error
When posting code please wrap it in [code] tags [/code] so it retains its formatting.
getSource is a method, not an attribute of ActionEvent, so you need ().Please do not ask for code as refusal often offends.
** This space for rent **
- 07-31-2013, 06:52 PM #3
Member
- Join Date
- Jul 2013
- Posts
- 13
- Rep Power
- 0
Re: Cannot Find Symbol Error
I'm so sorry I don't understand..
- 07-31-2013, 06:59 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Cannot Find Symbol Error
You're treating getSource as a field/attribute/variable, not a method. So you need closing ().
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-31-2013, 07:03 PM #5
Member
- Join Date
- Jul 2013
- Posts
- 13
- Rep Power
- 0
Re: Cannot Find Symbol Error
Is it like this?
[if(e.getSource() == btn1)]
- 07-31-2013, 07:11 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Cannot Find Symbol Error
Yep, but skip the square brackets.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-31-2013, 07:16 PM #7
Member
- Join Date
- Jul 2013
- Posts
- 13
- Rep Power
- 0
Re: Cannot Find Symbol Error
I had done that but i got this error
Name1.java:35: error: method addActionListener in class AbstractButton cannot be
applied to given types;
btn1.addActionListener(this);
^
required: ActionListener
found: Name1
reason: actual argument Name1 cannot be converted to ActionListener by method
invocation conversion
1 error
- 07-31-2013, 07:26 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Cannot Find Symbol Error
It's told you the problem.
addActionListener() requires an ActionListener as the parameter.
You haven't given it an ActionListener (ie Name1 does not implement ActionListener).
And if you do not understand what that means then you need to at least go through the Java Basics part of the tutorial linked in jim's signature above.Please do not ask for code as refusal often offends.
** This space for rent **
- 07-31-2013, 07:29 PM #9
Member
- Join Date
- Jul 2013
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Cannot find symbol error
By abdulahadone in forum New To JavaReplies: 11Last Post: 03-23-2012, 12:45 PM -
Cannot Find Symbol error
By mrgreenacid in forum New To JavaReplies: 13Last Post: 05-16-2011, 08:28 AM -
Cannot Find Symbol Error
By javadummy1 in forum New To JavaReplies: 6Last Post: 04-09-2011, 11:13 AM -
Cannot find symbol error
By rajivjoshi in forum New To JavaReplies: 3Last Post: 05-31-2010, 11:13 AM -
cannot find symbol symbol :constructor Error. Please help! =(
By KalEl in forum New To JavaReplies: 9Last Post: 10-18-2008, 09:26 PM
Bookmarks