Re: Need time waster code or something.



Taunto wrote:
I'm trying to "flash" a button, by changing its icon from one color to another.

It works with the mouse events (on when clicked, off when released), but when I try to run the following code, I don't see a change. I see something happen to the button (the color square gets ever so slightly smaller), but the color doesn't change. If I comment out the code where it sets the color back off, it stays on, so I know its executing.

I'm assuming my for-loop wait mechanism is the problem, and might be going by so fast I can't see it. I've tried larger numbers and the same problem.

Is there a better wait mechanism?


public void flashButton()
{
button.setIcon(onIcon);
for (long i = 0; i < 1000000; i++)
{

}
button.setIcon(offIcon);
}

You can't do it that way. You need a timer or a thread that sleeps. Here is an example of a JLabel that I wrote that displays the time. You should be able to get where you want to go from this.

package com.knutejohnson.components;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class JTimeLabel extends JLabel implements ActionListener {
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
private javax.swing.Timer timer = new javax.swing.Timer(250,this);

public JTimeLabel() {
super();
timer.start();
}

public JTimeLabel(String text) {
super(text);
timer.start();
}

public JTimeLabel(String text, int horizontalAlignment) {
super(text,horizontalAlignment);
timer.start();
}

public JTimeLabel(String text, int horizontalAlignment, String pattern) {
super(text, horizontalAlignment);
this.sdf = new SimpleDateFormat(pattern);
timer.start();
}

public void actionPerformed(ActionEvent ae) {
setText(sdf.format(new Date()));
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JTimeLabel("24:00:00",JLabel.CENTER));
f.pack();
f.setVisible(true);
}
}

--

Knute Johnson
email s/nospam/knute/
.