Solved

setText()

Posted on 2004-10-01
7
318 Views
Last Modified: 2012-06-27
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]
0
Comment
Question by:DrWarezz
  • 5
7 Comments
 
LVL 4

Expert Comment

by:gdrnec
Comment Utility
Might be wrong here but I think that if you sleep the current thread, you sleep the thread that repaints the component.

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.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Try

javax.swing.Timer timer = new javax.swing.Timer(1000, new Incrementer());
timer.start();


class Incrementer implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if (++x >= 3) {
            timer.cancel();
        }
        textField.setText("" + x);
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Oh that code should have started with

int x = 0;
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
.... or to be more precise

x = 0; (where 'x'is an instance variable)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
Comment Utility
My code was a bit messy, so:

javax.swing.Timer timer = new javax.swing.Timer(1000, new Incrementer());
timer.start();


class Incrementer implements ActionListener {
    int invocations;
    public void actionPerformed(ActionEvent e) {
        if (++invocations >= 3) {
            timer.cancel();
        }
        textField.setText("" + invocations);
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now