Solved

ACTIONLISTENER DOES NOT WORKING

Posted on 2001-06-22
19
288 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows 10 IE Certificate Issue 10 51
servlet filter example 37 72
Tagging and Merging on Branch 1 30
American Express @Work site and Java 4 29
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

832 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