?
Solved

setText()

Posted on 2004-10-01
7
Medium Priority
?
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 4

Expert Comment

by:gdrnec
ID: 12201353
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
ID: 12201674
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
ID: 12201683
Oh that code should have started with

int x = 0;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 12201690
.... or to be more precise

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

Expert Comment

by:Mayank S
ID: 12206681
>> 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 80 total points
ID: 12206700
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
ID: 12206933
8-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

765 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