Solved

setText()

Posted on 2004-10-01
7
337 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

696 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