Java mouseEvent timed setText?

Associate
Joined
21 Nov 2006
Posts
716
Location
Dublin
Hi all,

I have a mouseEvent that when you hover on a label it changes the text on the label to let you know that the mouse is over the label.

Is there any way I can have the new text show for like 5 seconds and then revert back to the original text :confused:

Here's the code,I need something to replace the comment:

Code:
JLabel label2 = new JLabel("Mouse Entered Label", SwingConstants.CENTER);
.
.
.
label2.addMouseListener(this);
.
.
.


             public void mouseEntered(MouseEvent e)
             { //start of mouseEntered

                    if(e.getSource() == label2)
                    {
                    label2.setText("Mouse Enter Recieved");
                    /* revert text to original after 5 seconds */
                    }

             } //end of mouseEntered


Thanks!
 
As above. Spawn a new thread to do it. And buy some glue to stick your hair back on after you've worked with multi-threading for a while :p
 
Something like this should do the trick

Code:
@Override
public void mouseEntered(MouseEvent e) {
	if(e.getSource() == label2) {
		String initialText = label2.getText();
		label2.setText("New Text");
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(5000);
					label2.setText(initialText);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}							
			}							
		}).start();
	}	
}
 
Back
Top Bottom