1

My goal is creating a function that waits half second, set the jbutton's background to red for one second, and after this second the jbutton will return to normal. Cant make this work.. This is my Function

 private void paint(final int num){
        Timer timer = new Timer(500, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (num == 1){
                btn.setBackground(Color.black);
                }
            }
        });
        timer.start();
    }
BarrySW19
  • 3,759
  • 12
  • 26
Erez
  • 502
  • 3
  • 6
  • 17

2 Answers2

1

Start a 500ms timer that will do two things when it goes off:
- change the color to red
- start a 1s timer that will change the color to normal, when it goes off

alterfox
  • 1,675
  • 3
  • 22
  • 37
0

Well, this would do it (note, you would probably want to put the code from the first sleep onwards into a timer or it's own thread in a real application to avoid blocking the thread running the code):

public static void main(String[] args) throws Exception {
    final JFrame jf = new JFrame();
    final JButton jButton = new JButton("Hello");
    final Color originalBackground = jButton.getBackground();

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            jf.getContentPane().add(jButton);
            jf.pack();
            jf.setVisible(true);
        }
    });

    Thread.sleep(500);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jButton.setBackground(Color.RED);
        }
    });
    Thread.sleep(1000);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jButton.setBackground(originalBackground);
        }
    });
BarrySW19
  • 3,759
  • 12
  • 26
  • 1
    No it wouldn't, because it's not running in the UI thread. Only the stuff inside the invokeLater() is. The sleeps are in the main thread. – BarrySW19 Oct 22 '14 at 08:50
  • Well in reality it probably would. You created an example that works, but that cannot be used in real life. You wouldn't really block the main thread so a button would change it's color? – alterfox Oct 22 '14 at 09:03
  • 1
    No, it's not running in the UI thread so it won't block it. If you don't want the main thread to block it's pretty trivial to put that code section into a timer or it's own thread. I'm trying to illustrate how it can be done without extra clutter. – BarrySW19 Oct 22 '14 at 09:14
  • "it's pretty trivial to put that code section into a timer or it's own thread" - then why not write it like it is supposed to be? "I'm trying to illustrate how it can be done without extra clutter." - no, it cannot be done like that, it is not a "clutter" issue. Especially if you write: "Well, this would do it:" in your answer. This, definitely wouldn't *do it*. – alterfox Oct 22 '14 at 09:19
  • 1
    Try running it - I think you'll find it does do it. – BarrySW19 Oct 22 '14 at 09:33
  • 1
    @alterfox That code will block the *main* thread, but not the *UI* thread. I understand your points here, and I think I agree; but you should try to word things more politely, and a little less hyperbolically. I'd also suggest a short example in your own answer to illustrate, mostly for future readers. – Andrew Barber Oct 22 '14 at 18:28
  • This example code blocks main thread. That sucks. That even doesn't matter, since in a normal Swing application, all things related to UI would be done on EDT (*UI thread* here). This behavior of changing button color would probably be triggered by some event (fired and handled on EDT), or in some other way (almost certainly on EDT). So this code would block EDT. That sucks. And matters. Thread.sleep has its purpose of existence. This not being one of those. My polite regards. – alterfox Oct 23 '14 at 13:27