Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing JFrame Text Periodically

Status
Not open for further replies.

napa1m

Programmer
Jul 24, 2000
39
0
0
TH
Hi! I've just spent full 4 days on this - to NO avail!!! All I want is a program that waits 1 minute before printing out the number of minutes its been active into a JFrame. IE after the first minute, the JFrame displays 1, after 2, it displays 2, and so on and so forth.

If I call the JFrame's constructor and updateNo methods from a differnt class, it works fine, so I know for a fact that its NOT anything to do with the JFrame. THE JFRAME IS FINE!!!

So, I've been trying to use a Thread, and I can get the program to sleep() for 1 second, but it never displays the JFrame until the program stops looping (ie, if i restrict it to say, 5 minutes), even tho I use setVisible(true), repaint(), etc. If you can get some code that actually WORKS, please post it!!! PLEASE!!! I BEG OF YOU!!!

PLEASE!!! napa1m
hansen@ithsite.com
 

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;


public class Actualizable extends JFrame implements Runnable
{
JLabel time=new JLabel();
JButton button=new JButton("Boton");
Thread t=new Thread(this);
int tiempo=1;

public void run(){
while(true){
try{
t.sleep(100);
time.setText(""+(tiempo++));
repaint();
}catch(InterruptedException e){}
}
}

public Actualizable(){
time.setBounds(10,10,100,20);
button.setBounds(10,40,100,20);
getContentPane().setLayout(null);
getContentPane().add(time);
getContentPane().add(button);
t.start();
}

public static void main(String[] args)
{
Actualizable a=new Actualizable();
a.setBounds(10,10,200,200);
a.show();
}
}



If I understood you well, this code works. I put a button to see if the JFrame responds in the meantime, and it works. Just adjust the sleeptime to 60000 to wait for 1 minute.
But, if you want to show a frame to witness the status of a long task for example, it is more difficult. I had been stuck for a month with a similar problem. Write me if your problem is something like that. (pedro.solorzano@terra.com.co)

Hope it helps-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top