0

i have a problem with setting background color of Jtogglebutton dynamically. I want Jtogglebutton blink like a led, on and off at a decised time, like 500ms. I tried to override paint and paintComponent method too. But couldn't succeed either. I'm stuck. Here is my code thanks for help.

Led class:

public class Led extends JToggleButton {
private Color okColor = Color.GREEN;
private Color notOkColor = Color.RED;
private static int BLINK_FREQUENCY=500;

public Led() {
    this.setPreferredSize(new Dimension(50, 50));
    timer.start();
}

Timer timer=new Timer(BLINK_FREQUENCY, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
            setBackground(okColor);
            System.out.println("ok");
            try {
                Thread.sleep(BLINK_FREQUENCY);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            setBackground(notOkColor);
            System.out.println("notok");
    }
});

}

MainFrame Class:

public class MainFrame {

private JFrame frame;
private Led led;
private JPanel panel;

public MainFrame() {
    initializeComponents();
}

private void initializeComponents() {
    frame = new JFrame("Blinking Led");
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    {
        panel = new JPanel();
        led = new Led();
        panel.add(led);
        frame.add(panel);
    }

}

public void setVisible(boolean visible) {
    frame.setVisible(visible);
}

}

f.mike
  • 5
  • 2
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Nov 13 '16 at 08:46
  • I'm not sure I understand right, you want the color to switch every 500 ms or you want the state of the button to switch and then trigger the color switch? – M B Nov 13 '16 at 09:00
  • I want to switch color of button every 500ms. State is not important. – f.mike Nov 13 '16 at 09:05
  • Why would you `sleep` inside action listener? – Antoniossss Nov 13 '16 at 09:12

2 Answers2

1

It is almost done:

    Timer timer=new Timer(BLINK_FREQUENCY, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
          setBackgroundColor(getBackgroundColor()==okColor ? noOkColor:okColor);
    }
    });

timer.start();
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
-1

I don't see the point in using the Timer class but just a simple thread should work

public Led() {
    this.setPreferredSize(new Dimension(50, 50));
    thread.start();
}

Thread  thread  = new Thread(() -> {
                    while (true) {
                        if (getBackground().equals(notOkColor)) {
                            setBackground(okColor);
                        } else {
                            setBackground(notOkColor);
                        }
                        try {
                            Thread.sleep(BLINK_FREQUENCY);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
M B
  • 307
  • 2
  • 8
  • :) Thanks. it helped me a lot. Do you have any idea to make it with paint method? – f.mike Nov 13 '16 at 09:15
  • @Antoniossss No there is absolutely no point in using a class that provides you with an ActionListener and ActionEvent that you won't ever use in your code. The correct way is to manually invoke revalidate() and repaint() on EDT using SwingUtils.invokeLater() but in this case not even that is required. – M B Nov 13 '16 at 09:18
  • @f.mike You want to update the background from inside the paint() method of your other class, MainFrame? – M B Nov 13 '16 at 09:22
  • i tried both. updating inside of Led and MainFrame. No matter how for me. – f.mike Nov 13 '16 at 09:24