Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: add action listener problem

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default add action listener problem

    i have getting errors when i try to compile this code relating to ACTIONLISTENER . i use JDK1.3


    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    class temp extends Frame
    {
    Button b1,b2;
    Label l1,l2;
    TextField t1,t2;
    public temp (String t)
    {
    super(t);
    b1=new Button("CONVERT");
    b2=new Button("CANCEL");
    l2=new Label("TEMPRETURE IN C");
    l1=new Label("TEMPRETURE IN F");
    t1=new TextField("20");
    t2=new TextField("20");
    setLayout (new FlowLayout());
    add(l1);
    add(t1);
    add(l2);
    add(t2);
    add(b1);
    add(b2);
    t2.setEditable(false);
    setSize(500,500);
    setVisible (true);
    b1.addActionListener(this);
    b2.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b1)
    {
    float a=Float.parseFloat(t1.getText());
    float b;
    b=(9/5*(a+32));
    t2.setEditable(true);
    t2.setText (String.valueOf(b));
    t2.setEditable(false);
    show();
    }
    if(e.getSource()==b2)
    {
    System.exit (0);
    } } }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: add action listener problem

    i have getting errors
    What errors? Post them in their entirety (and read the link in my signature entitled 'getting help' if you want to make the most of these forums)

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: add action listener problem

    these are the errors on compilation . i use jdk1.3
    Attached Images Attached Images

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: add action listener problem

    b1.addActionListener(this);
    b2.addActionListener(this);

    these 2 addActionListeners(this) or the reserved word 'this' refer to your class's instance(a temp instance), not to an instance of an ActionListener interface or a class that implements an ActionListener interface

    better start from here
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Last edited by chronoz13; January 29th, 2012 at 06:26 AM.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: add action listener problem

    Quote Originally Posted by chronoz13 View Post
    b1.addActionListener(this);
    b2.addActionListener(this);

    these 2 addActionListeners(this) or the reserved word 'this' refer to your class's instance(a temp instance), not to an instance of an ActionListener interface or a class that implements an ActionListener interface

    better start from here
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    great thanks .

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: add action listener problem

    you should first implement ActionListener to use actionEvent.

Similar Threads

  1. Action Listener?user input problem
    By gpelefty90 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 2nd, 2011, 05:17 AM
  2. Problem with JText Fields and using action listener
    By toble in forum AWT / Java Swing
    Replies: 2
    Last Post: October 27th, 2010, 04:44 PM
  3. converter program, problem with action listener
    By robertson.basil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:44 AM
  4. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  5. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM