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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JLabel Hide

Status
Not open for further replies.

eh171

Programmer
Feb 21, 2003
9
GB
What I am trying to do is display a JLabel and then after 5 seconds I wish to hide the label. I have the following code but it does not work:
label.setText("hi")
try {Thread.sleep(5000);} catch (InterruptedExecution e){}
label.setText(null)

I also tried repaint but didnt work:
label.setText("hi");
label.setText("");
repaint(5000);
 
I think the first way will work if you put the timer and reset of the label in another thread:

import javax.swing.*;
import java.awt.*;

public class Frame4 extends JFrame
{
public JLabel jLabel1 = new JLabel();

public Frame4()
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 300));
jLabel1.setText("Hello");
jLabel1.setBounds(new Rectangle(35, 35, 165, 30));
this.getContentPane().add(jLabel1, null);

Thread t = new Thread( new Timert(this) );
t.start();
}
}

class Timert extends Thread
{
Frame4 f = null;
public Timert( Frame4 frame)
{
f = frame;
}

public void run()
{
try { sleep(5000); } catch (Exception e) {}
f.jLabel1.setText("Goodbye");
f.show();
}
}
"When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top