Solved

setText()

Posted on 2004-10-01
7
330 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
Industry Leaders: 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 20 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
null output 3 43
Running JavaFX on JDeveloper 12C 1 79
eclipse console opening separately 2 38
Find out all repos that a user is most active on Github 1 33
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 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