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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

swing and passing values from a JTextField

Status
Not open for further replies.

Erikxxx

Programmer
May 5, 2003
49
GB
Hi all,

I'm new to Swing programming and have the following newbie question.

I've got a simple class that has got two JTextFields and a submit button. I've decided to implement the Listener in a totally different class. The problem is that I want to pass the value from my JTextField into my listener class, which then is going to send this data to some other class(for manipulation or storage in DB).
I've had the MVC concept in mind when I started with this. Well, it might be wrong passing the values of my textfields to the Listener class and then pass them on to somewhere else. Have anyone got any suggestions on how to solve this or even a better approach on how to handle this kind of issues?

Best Regards
Erik
 
This is a link to web page about how to generate an event and catch it.
// In file eventgui/ex1/TelephoneEvent.java
public class TelephoneEvent
extends java.util.EventObject {
public TelephoneEvent(Telephone source) {
super(source);
}
}
// In file eventgui/ex1/TelephoneAdapter.java
public class TelephoneAdapter implements TelephoneListener {
public void telephoneRang(TelephoneEvent e) {
}
public void telephoneAnswered(TelephoneEvent e) {
}
}
// In file eventgui/ex1/Telephone.java
import java.util.Vector;
public class Telephone {
private Vector telephoneListeners = new Vector();
public void ringPhone() {
fireTelephoneRang();
}
public void answerPhone() {
fireTelephoneAnswered();
}
public synchronized void addTelephoneListener(
TelephoneListener l) {
if (telephoneListeners.contains(l)) {
return;
}
telephoneListeners.addElement(l);
}
public synchronized void removeTelephoneListener(
TelephoneListener l) {
telephoneListeners.removeElement(l);
}
private void fireTelephoneRang() {
Vector tl;
tl = (Vector) telephoneListeners.clone();
int size = tl.size();
if (size == 0) {
return;
}
TelephoneEvent event = new TelephoneEvent(this);
for (int i = 0; i < size; ++i) {
TelephoneListener listener =
(TelephoneListener) tl.elementAt(i);
listener.telephoneRang(event);
}
}
private void fireTelephoneAnswered() {
Vector tl;
tl = (Vector) telephoneListeners.clone();
int size = tl.size();
if (size == 0) {
return;
}
TelephoneEvent event = new TelephoneEvent(this);
for (int i = 0; i < size; ++i) {
TelephoneListener listener =
(TelephoneListener) tl.elementAt(i);
listener.telephoneAnswered(event);
}
}
}
// In file eventgui/ex1/AnsweringMachine.java
public class AnsweringMachine
implements TelephoneListener {
public void telephoneRang(TelephoneEvent e) {
System.out.println("AM hears the phone ringing.");
}
public void telephoneAnswered(TelephoneEvent e) {
System.out.println("AM sees that the phone was answered.");
}
}
// In file eventgui/ex1/Person.java
public class Person {
public void listenToPhone(Telephone t) {
t.addTelephoneListener(
new TelephoneAdapter() {
public void telephoneRang(TelephoneEvent e) {
System.out.println("I'll get it!");
}
}
);
}
}
// In file eventgui/ex1/Example1.java
public class Example1 {
public static void main(String[] args) {
Telephone ph = new Telephone();
Person bob = new Person();
AnsweringMachine am = new AnsweringMachine();
ph.addTelephoneListener(am);
bob.listenToPhone(ph);
ph.ringPhone();
ph.answerPhone();
}
}
 
Hi,
Thanks for your reply. I'll have a look at it and I might have to come back with questions.

Thanks anyway
Erik
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top