Results 1 to 9 of 9
  1. #1
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default 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();
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

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

  3. #3
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default Re: Cannot Find Symbol Error

    I'm so sorry I don't understand..

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Cannot Find Symbol Error

    You're treating getSource as a field/attribute/variable, not a method. So you need closing ().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default Re: Cannot Find Symbol Error

    Is it like this?

    [if(e.getSource() == btn1)]

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Cannot Find Symbol Error

    Yep, but skip the square brackets.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default 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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

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

  9. #9
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default Re: Cannot Find Symbol Error

    Oh ..i forgot to implement it. thanks it do works :)

Similar Threads

  1. Cannot find symbol error
    By abdulahadone in forum New To Java
    Replies: 11
    Last Post: 03-23-2012, 11:45 AM
  2. Cannot Find Symbol error
    By mrgreenacid in forum New To Java
    Replies: 13
    Last Post: 05-16-2011, 07:28 AM
  3. Cannot Find Symbol Error
    By javadummy1 in forum New To Java
    Replies: 6
    Last Post: 04-09-2011, 10:13 AM
  4. Cannot find symbol error
    By rajivjoshi in forum New To Java
    Replies: 3
    Last Post: 05-31-2010, 10:13 AM
  5. Replies: 9
    Last Post: 10-18-2008, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •