Flash for a second

Hello people,

Am trying to get a JLable to flash red for 1 second when the text changes. But am finding it very hard to figure out how to do it.

Here is an example of a JLabel that listens for a change and tries to flash red for 1 second, but its not working.

import java.beans.PropertyChangeListener;

import java.awt.*;

import java.awt.event.*;

publicclass TextListenerTesterextends Frameimplements ActionListener, PropertyChangeListener{

TextField t;

Label l;

public TextListenerTester ( String s ){

super ( s ) ;

setLayout (new FlowLayout() ) ;

t =new TextField ( ) ;

l =new Label("hello");

t.setSize ( 100,100 ) ;

t.setLocation ( 50,50 ) ;

add ( t ) ;

add ( l ) ;

t.addActionListener (this ) ;

l.addPropertyChangeListener(this);

}

publicvoid actionPerformed ( ActionEvent e ){

l.setText(t.getText() ) ;

}

publicvoid propertyChange(java.beans.PropertyChangeEvent evt){

//l.setBackground(Color.red); // here to flash for 1 second

//repaint();

System.out.println("I have changed");

}

publicstaticvoid main ( String [ ] args ){

TextListenerTester textListenerTester =new TextListenerTester ("Text Listener Tester" ) ;

textListenerTester.setSize ( 400,400 ) ;

textListenerTester.setVisible (true ) ;

}

}

Please help me out guys.

Thanks

[2654 byte] By [omgeganetworka] at [2007-11-27 11:44:51]
# 1

could you subclass JLabel and override the setText method to flash when called?

petes1234a at 2007-7-29 17:58:17 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks, I have got it working now.

But does anyone know the color code for Yellow?

new Color(?,?,?)

Thanks for your helps

omgeganetworka at 2007-7-29 17:58:17 > top of Java-index,Java Essentials,New To Java...
# 3

> Thanks, I have got it working now.

> But does anyone know the color code for Yellow?

Color.YELLOW

~

yawmarka at 2007-7-29 17:58:17 > top of Java-index,Java Essentials,New To Java...
# 4

How did you get it to work? Let's see your code. I had to try myself, and this is what I came up with. I created a subclass of JLabel and called a Thread.sleep method in a separate SwingWorker thread:

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class FlashingLabel extends JFrame

{

private MyLabel fooLabel;

private String[] fooLabelTexts = {"Foo Label 1", "Foo Label 2",

"Foo Label 3", "What the heck?!?"};

private MySwingWorker mySwingWorker;

public FlashingLabel()

{

super("My Swing App");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getContentPane().add(myPanel());

pack();

}

private JPanel myPanel()

{

JPanel fooPanel = new JPanel();

GridLayout myGridLO = new GridLayout(0, 1);

myGridLO.setHgap(5);

myGridLO.setVgap(5);

fooPanel.setLayout(myGridLO);

fooPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JButton button = new JButton("Button");

fooPanel.add(button);

button.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

btnAction(ae);

}

});

fooLabel = new MyLabel(fooLabelTexts[0]);

fooPanel.add(fooLabel);

return fooPanel;

}

private void btnAction(ActionEvent ae)

{

String fooStr = fooLabel.getText();

int indx = -1;

for (int i = 0; i < fooLabelTexts.length; i++)

{

if (fooStr.equals(fooLabelTexts[i]))

{

indx = i;

}

}

indx++;

indx %= fooLabelTexts.length;

fooLabel.setText(fooLabelTexts[indx]);

}

private static void createAndShowGUI()

{

FlashingLabel myBriefSwing = new FlashingLabel();

myBriefSwing.setVisible(true);

}

public static void main(String[] args)

{

javax.swing.SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

createAndShowGUI();

}

});

}

class MyLabel extends JLabel

{

private Color bkgdColor = null;

private boolean opaque;

public MyLabel()

{

super();

}

public MyLabel(String s)

{

super(s);

}

public void resetColor()

{

super.setOpaque(opaque);

super.setBackground(bkgdColor);

super.revalidate();

}

@Override

public void setText(String s)

{

// set only once, first time called

if (bkgdColor == null)

{

bkgdColor = super.getBackground();

opaque = super.isOpaque();

}

super.setBackground(Color.RED);

super.setOpaque(true);

super.setText(s);

super.revalidate();

mySwingWorker = new MySwingWorker();

mySwingWorker.execute();

}

}

class MySwingWorker extends SwingWorker<Void, Void>

{

@Override

protected Void doInBackground() throws Exception

{

Thread.sleep(1000);

return null;

}

@Override

public void done()

{

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

fooLabel.resetColor();

}

});

}

}

}

Message was edited by:

petes1234

petes1234a at 2007-7-29 17:58:17 > top of Java-index,Java Essentials,New To Java...
# 5

Shoot.... I looked at the OP's question about a JLabel, but not at the OP's code which is all about AWT, Frame, and Label. I'm confused...

petes1234a at 2007-7-29 17:58:17 > top of Java-index,Java Essentials,New To Java...