help with Events, Listeners

S

stu

I am a new proggramer and I am having a hard time understanding how to
delevlop my code to read what is in my text boxes and what option has
been submitted and creat another window when the user selects "Done".
How do I get a Listener to go back and grab all the info that the user
inputted and display it when the user is finished??

-Stu
 
K

Knute Johnson

stu said:
I am a new proggramer and I am having a hard time understanding how to
delevlop my code to read what is in my text boxes and what option has
been submitted and creat another window when the user selects "Done".
How do I get a Listener to go back and grab all the info that the user
inputted and display it when the user is finished??

-Stu

Stu:

Here is a simple example. There are three listeners in this code. The
first is the WindowListener to end the program when you click on the X
in the window Frame. It uses a WindowAdapter so that you don't have to
override all of the methods of the interface WindowListener. The second
is the ActionListener. In the first line of code for the class you'll
see that it implements ActionListener. This means that this class has
all of the methods of the ActionListener interface in the code for the
class. The ActionListener is added to the button with the
Button.addActionListener(). The 'this' refers it to the code of the
class for the methods rather than adding the ActionListener code right
there, although it could be done that way too. The last listener is
another WindowListener added to the Dialog that displays our TextField
entry when we click the Button.

When you get started on your code, if you have more questions, which you
will, post it and your code and I'm sure you can get answers here.

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

public class Listen extends Frame implements ActionListener {
TextField tf;

public Listen() {
super("Listen");

setLayout(new FlowLayout());

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});

tf = new TextField("Enter your text here");
add(tf);

Button b = new Button("Show");
b.addActionListener(this);
add(b);

pack();
setVisible(true);
}

public void actionPerformed(ActionEvent ae) {
final Dialog d = new Dialog(Listen.this,"Here it is!",true);
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
d.dispose();
}
});
// get the text in your TextField
Label l = new Label(tf.getText());
d.add(l);
d.pack();
d.setVisible(true);
}

public static void main(String[] args) {
new Listen();
}
}
 
R

Roedy Green

I am a new proggramer and I am having a hard time understanding how to
delevlop my code to read what is in my text boxes and what option has
been submitted and creat another window when the user selects "Done".
How do I get a Listener to go back and grab all the info that the user
inputted and display it when the user is finished??

see http://mindprod.com/jgloss/event11.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top