Solved

setText()

Posted on 2004-10-01
7
323 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 78
Apps blocked by Java 9 84
SHA2 certs for IIS AND Java? 2 97
Html Table Looping (part 2) 5 11
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …

832 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