Solved

ACTIONLISTENER DOES NOT WORKING

Posted on 2001-06-22
19
281 Views
Last Modified: 2012-05-04
I created a frame,button and textfield.
i add the actionlistener.
button.addActionlistener(this);
in that frame one textfield and button are available.

if repeat this execution over a loop(ie i created more than 2) the last created frame only obey the actionlistener control.

i gave that if i click the button it shows some list.
but if i try the last one only shows the list.others doesn't obey for the listener.
please clarify my doubts i don'w want seperate class. I need same class(that is only one class, Applet
Thanking you for your best cooperaiton
bye bye
Madhusudhanan



0
Comment
Question by:anmadhu
19 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 6217330
post ur code pls
RJ
0
 

Expert Comment

by:johnbrewer1980
ID: 6217345
Need more info.

However, make sure that you're adding a different instance of your actionlistener class to each button.
0
 

Author Comment

by:anmadhu
ID: 6217382
Button b;
TextField tf;
Frame a;

loop begin:
b=new Button("hello");
tf=new TextField("hello",tf);
a=new Frame("frame");
b.addActionListener(this)
a.add("North",b);
end degin;

public void actipnPerformed(ActionEvent e){
if(e.getSource() == b){ //line no 50
//some action like
a.setVisible(false);

}
}

if execute this loop g times i gor 5 frames , each frame with one button and one textfield. if i click the button ,
the 5th button only execute the  line no 50(some action like).

i want to eecute this lines for all the buttons
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6217548
import java.awt.*;
import java.awt.event.*;

public class test {
  public test() {
    for(int i = 0; i<5; i++) {
      new MyFrame("Hello", i);
    }
  }

  class MyFrame extends Frame {
    Button b;
    TextField tf;
    int instanceNumber;

    public MyFrame(String title, int instNo) {
      super(title);
      instanceNumber = instNo;
      b = new Button("Hello" + " " + instNo);
      tf = new TextField("hello you");
      b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){
          if(e.getSource() instanceof Button){ //line no 50
            System.out.println(e.getActionCommand());
            setVisible(false);
          }
        }
      });
      add("North",b);
      add("Center", tf);
      setSize(100, 100);
      setLocation(instNo*40, instNo*40);
      setVisible(true);
    }
  }

public static void main(String[] args) {
  new test();
}
}
0
 

Author Comment

by:anmadhu
ID: 6217769
Mr Ovi I user that code in my program. I got compile time error.
The Error is : interface java.awt.event.ActionListener is an interface. It can't be instantiated.
for creating the instance of listener
//i.e new ActionListener()
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6217881
You are using an old JDK I supose. Use this :
import java.awt.*;
import java.awt.event.*;

public class test {
 public test() {
   for(int i = 0; i<5; i++) {
     new MyFrame("Hello", i);
   }
 }

 class MyFrame extends Frame implements ActionListener {
   Button b;
   TextField tf;
   int instanceNumber;

   public MyFrame(String title, int instNo) {
     super(title);
     instanceNumber = instNo;
     b = new Button("Hello" + " " + instNo);
     tf = new TextField("hello you");
     b.addActionListener(this);
     add("North",b);
     add("Center", tf);
     setSize(100, 100);
     setLocation(instNo*40, instNo*40);
     setVisible(true);
   }
 }

 public void actionPerformed(ActionEvent e){
   if(e.getSource() instanceof Button){ //line no 50
     System.out.println(e.getActionCommand());
     setVisible(false);
   }
 }

public static void main(String[] args) {
 new test();
}
}
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6217905
You are using an old JDK I supose. Use this :
import java.awt.*;
import java.awt.event.*;

public class test {
 public test() {
   for(int i = 0; i<5; i++) {
     new MyFrame("Hello", i);
   }
 }

 class MyFrame extends Frame implements ActionListener {
   Button b;
   TextField tf;
   int instanceNumber;

   public MyFrame(String title, int instNo) {
     super(title);
     instanceNumber = instNo;
     b = new Button("Hello" + " " + instNo);
     tf = new TextField("hello you");
     b.addActionListener(this);
     add("North",b);
     add("Center", tf);
     setSize(100, 100);
     setLocation(instNo*40, instNo*40);
     setVisible(true);
   }
 }

 public void actionPerformed(ActionEvent e){
   if(e.getSource() instanceof Button){ //line no 50
     System.out.println(e.getActionCommand());
     setVisible(false);
   }
 }

public static void main(String[] args) {
 new test();
}
}
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6217943
You are using an old JDK I supose. Use this :
import java.awt.*;
import java.awt.event.*;

public class test {
 public test() {
   for(int i = 0; i<5; i++) {
     new MyFrame("Hello", i);
   }
 }

 class MyFrame extends Frame implements ActionListener {
   Button b;
   TextField tf;
   int instanceNumber;

   public MyFrame(String title, int instNo) {
     super(title);
     instanceNumber = instNo;
     b = new Button("Hello" + " " + instNo);
     tf = new TextField("hello you");
     b.addActionListener(this);
     add("North",b);
     add("Center", tf);
     setSize(100, 100);
     setLocation(instNo*40, instNo*40);
     setVisible(true);
   }
 }

 public void actionPerformed(ActionEvent e){
   if(e.getSource() instanceof Button){ //line no 50
     System.out.println(e.getActionCommand());
     setVisible(false);
   }
 }

public static void main(String[] args) {
 new test();
}
}
0
 

Author Comment

by:anmadhu
ID: 6217973
Mr Ovi,
 Still there.
now the compile time error
The inner class test11. MyFrame is an abstract class. It can't be instantiated.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:Ovi
ID: 6218001
MR. Anmadhu :

As you can see mai program is called 'public class test' and has a method main(...). This meend that you must copy the code from one of my comments and save'it in a file test.java. After that compile the filetest.java (javac test.java) and runn'it (java test). On my computer the two version posted by me are working fine !!!
Don't try to put this into your code, because is made as a stand alone program !!! If you still want to do that, post all of your original code here and I will make'it for you. This is thelast comment I post here now in the case you are not still able to run the code (In this case please read first an Java tutorial about this topic).
0
 
LVL 9

Accepted Solution

by:
Ovi earned 30 total points
ID: 6218006
Sorry I put the actionPerformed method out of the inner class. Use this :

import java.awt.*;
import java.awt.event.*;

public class test {
public test() {
  for(int i = 0; i<5; i++) {
    new MyFrame("Hello", i);
  }
}

class MyFrame extends Frame implements ActionListener {
  Button b;
  TextField tf;
  int instanceNumber;

  public MyFrame(String title, int instNo) {
    super(title);
    instanceNumber = instNo;
    b = new Button("Hello" + " " + instNo);
    tf = new TextField("hello you");
    b.addActionListener(this);
    add("North",b);
    add("Center", tf);
    setSize(100, 100);
    setLocation(instNo*40, instNo*40);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e){
    if(e.getSource() instanceof Button){ //line no 50
      System.out.println(e.getActionCommand());
      setVisible(false);
    }
  }
}

public static void main(String[] args) {
new test();
}
}
0
 

Author Comment

by:anmadhu
ID: 6220801
Mr Ovi,
  I don't want to create seperate instance for myframe.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6223346
In your example, the action listener was working only for the last frame because you have used the same button instance. And if you read'it carefully, you create by your self another frame instance there.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6223436
Why ? If you have no explanation for me , maybe you will have for the technical suport people of this site. ... and you can loose your right to come in here. The anwear provided by me was your single solution to do the things right. If is not all the time possible to do in the way you want to be done this is another matter. Your fault !!! See you soon.
0
 

Author Comment

by:anmadhu
ID: 6230535
still i do not receive the proper solution(answer)
0
 

Author Comment

by:anmadhu
ID: 6230536
still i do not get the proper answer
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6259671
Then read a little more books my friend and seek for your solution there (if you'll find any). Bleah ...
0
 
LVL 5

Expert Comment

by:vemul
ID: 7621023
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- points to Ovi
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Expert Comment

by:modulo
ID: 7643103
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now