By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,738 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,738 IT Pros & Developers. It's quick & easy.

Really ignorant TextField (AWT) question from an idiot.

P: n/a

Help me, I'm suffering!

Situation: I have a load() function which loads a bunch
of TextFields with values. There are TextListeners
registered for each of these TextFields. Thus, changing the
text in any of them fires off a TextEvent, even if done
programmatically (by means of setText()).

The problem: I need all the various TextEvents fired off
during the load operation (as a result of using TextField.setText())
to be handled BEFORE the load() function completes. In other words,
load() must hang around waiting for all these events to have been
handled by somehow blocking on a state variable.

Some code:

public class Stupid extends Something implements TextListener
{
TextField lname = new TextField();
TextField fname = new TextField();

public Stupid()
{
lname.setName("LNAME");
fname.setName("FNAME");

lname.addTextListener(this);
fname.addTextListener(this);

load();
}

public void textValueChanged(TextEvent e)
{
System.out.println(paramString() + ": " + e.getSource().toString());
}

public void load()
{
System.out.println("LOADING...");

lname.setText("Quixote");
fname.setText("Don");

System.out.println("LOADED.");
}
}

Some output:

LOADING...
LOADED.
TEXT_VALUE_CHANGED: java.awt.TextField[LNAME,116,142,230x31,invalid,text=Quixote,editable ,selection=0-0]
TEXT_VALUE_CHANGED: java.awt.TextField[FNAME,116,173,230x31,invalid,text=Don,editable,sel ection=0-0]
What I want:

LOADING...
TEXT_VALUE_CHANGED: java.awt.TextField[LNAME,116,142,230x31,invalid,text=Quixote,editable ,selection=0-0]
TEXT_VALUE_CHANGED: java.awt.TextField[FNAME,116,173,230x31,invalid,text=Don,editable,sel ection=0-0]
LOADED.

Any ideas? I'm going mad here!
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
cd*@milhouse.sgiscpq.com (C. Armour) wrote in message news:<86************@milhouse.sgiscpq.com>...
Help me, I'm suffering!

Situation: I have a load() function which loads a bunch
of TextFields with values. There are TextListeners
registered for each of these TextFields. Thus, changing the
text in any of them fires off a TextEvent, even if done
programmatically (by means of setText()).

The problem: I need all the various TextEvents fired off
during the load operation (as a result of using TextField.setText())
to be handled BEFORE the load() function completes. In other words,
load() must hang around waiting for all these events to have been
handled by somehow blocking on a state variable.

Some code:

public void load()
{
System.out.println("LOADING...");

lname.setText("Quixote");
fname.setText("Don");

System.out.println("LOADED.");


Change this line to:

SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
System.out.println("LOADED.");
}
});

This will ensure that the Event Dispatching Thread has processed all
pending events prior to printing out the text. invokeLater will also
work in this case, since you don't have anything after the second
print that needs to happen synchronously.

Greg
Jul 17 '05 #2

P: n/a
>>>>> "Gregory" == Gregory A Swarthout <gr******@xmission.com> writes:

Gregory> Change this line to:

Gregory> SwingUtilities.invokeAndWait(new Runnable() { public
Gregory> void run() { System.out.println("LOADED."); } });

Gregory> This will ensure that the Event Dispatching Thread has
Gregory> processed all pending events prior to printing out the
Gregory> text. invokeLater will also work in this case, since you
Gregory> don't have anything after the second print that needs to
Gregory> happen synchronously.

Hey, thanks. That's exactly what I was looking for.

Of course, I'm not using Swing, but it should work, right?
cda

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.