DrWarezz
asked on
setText()
Hi all,
I have a pretty simple, but frustrating problem with my JTextFields:
I want to apply some text, then pause, then apply some more text, then pause, etc..
Here's an example:
textField.setText( "1" );
try { Thread.sleep( 1000 ); } catch (Exception e) {}
textField.setText( "2" );
try { Thread.sleep( 1000 ); } catch (Exception e) {}
textField.setText( "3" );
Now, what this will do is:
pause for 2000ms, then, set the text field as "3"!!
I've tried using the repaint() method after each setText() line, but it makes no difference..
How do I go about doing this?
Thanks in advance,
[r.D]
I have a pretty simple, but frustrating problem with my JTextFields:
I want to apply some text, then pause, then apply some more text, then pause, etc..
Here's an example:
textField.setText( "1" );
try { Thread.sleep( 1000 ); } catch (Exception e) {}
textField.setText( "2" );
try { Thread.sleep( 1000 ); } catch (Exception e) {}
textField.setText( "3" );
Now, what this will do is:
pause for 2000ms, then, set the text field as "3"!!
I've tried using the repaint() method after each setText() line, but it makes no difference..
How do I go about doing this?
Thanks in advance,
[r.D]
Try
javax.swing.Timer timer = new javax.swing.Timer(1000, new Incrementer());
timer.start();
class Incrementer implements ActionListener {
public void actionPerformed(ActionEven t e) {
if (++x >= 3) {
timer.cancel();
}
textField.setText("" + x);
}
}
javax.swing.Timer timer = new javax.swing.Timer(1000, new Incrementer());
timer.start();
class Incrementer implements ActionListener {
public void actionPerformed(ActionEven
if (++x >= 3) {
timer.cancel();
}
textField.setText("" + x);
}
}
Oh that code should have started with
int x = 0;
int x = 0;
.... or to be more precise
x = 0; (where 'x'is an instance variable)
x = 0; (where 'x'is an instance variable)
>> I think that if you sleep the current thread, you sleep the thread that repaints the component.
Exactly, you should not make the same thread sleep.
Exactly, you should not make the same thread sleep.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
8-)
Ih have done something similar for changing the current Time in a JLabel.
What I did was construct a small runnable whose responsibility it is to change the values of the JTextField independadly of the graphic thread.
e.g.
class Counter extends Thread {
JTextField field;
boolean breakCondition;
public Counter(JTextField field) {
this.field = field;
}
public void setBreakCondition(boolean flag) {
breakCondition = flag;
}
public void run() {
int i = 0;
while (!breakCondition) { // or break condition
field.setText("" + i);
this.sleep(1000);
}
}
}
I think this should work.