Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why we need "listioner"? 1

Status
Not open for further replies.

welldefined

Programmer
Mar 9, 2006
62
GB
Hi,

I tried following code and it looks fine. It does not need a "lestioner" to fire the return key press event.
1. Can you check if this code is OK?
2. If this code is OK, why we need to use "listioner"?

import java.applet.Applet;
import java.awt.*;

public class ListPrime extends Applet
{
Label Label1; TextField Text1;
public void init()
{
Label1=new Label("");
Text1=new TextField(4);
add(Label1);
add(Text1);
}
public boolean action(Event e, Object o)
{
if(e.target==Text1)
Label1.setText(Text1.getText());
return true;
}
}
 
Thank you for your comment.

Can you show me a better way to do what I did in my code?
 
The first link (will visit the second link later) you suggested is a great tutorial! After reading the example, I got a new question:

In the following code, the interface ActionListener is implemented and its method actionPerformed(ActionEvent evt) can be (or have to be) used in MyClass, fine. What I don't understand is
Button1.addActionListener(this);
How the method addActionListener(...) is declared in the interface ActionListener so that Button1 in MyClass got that method?

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class MyClass extends Applet implements ActionListener
{
Button Button1; Label Label1;
public void init()
{
Button1 = new Button();
Button1.addActionListener(this);
Label1 = new Label();
add(Button1);
add(Label1);
}
public void actionPerformed(ActionEvent evt)
{
Label1.setText("1111111111");
}
}
 
How the method addActionListener(...) is declared in the interface ActionListener so that Button1 in MyClass got that method?
Just look into the api-doc and you see:

Code:
    public void actionPerformed (ActionEvent evt);

(And do yourself a favor, and use lowercase initials for attributes like button1, label1.
Better: buttonOK, labelOK.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top