[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

ACTIONLISTENER DOES NOT WORKING

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
anmadhu
Asked:
anmadhu
1 Solution
 
rjackmanCommented:
post ur code pls
RJ
0
 
johnbrewer1980Commented:
Need more info.

However, make sure that you're adding a different instance of your actionlistener class to each button.
0
 
anmadhuAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
OviCommented:
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
 
anmadhuAuthor Commented:
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
 
OviCommented:
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
 
OviCommented:
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
 
OviCommented:
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
 
anmadhuAuthor Commented:
Mr Ovi,
 Still there.
now the compile time error
The inner class test11. MyFrame is an abstract class. It can't be instantiated.
0
 
OviCommented:
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
 
OviCommented:
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
 
anmadhuAuthor Commented:
Mr Ovi,
  I don't want to create seperate instance for myframe.
0
 
OviCommented:
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
 
OviCommented:
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
 
anmadhuAuthor Commented:
still i do not receive the proper solution(answer)
0
 
anmadhuAuthor Commented:
still i do not get the proper answer
0
 
OviCommented:
Then read a little more books my friend and seek for your solution there (if you'll find any). Bleah ...
0
 
vemulCommented:
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
 
moduloCommented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now