0

I had a question about making a JButton flash colors ,like you would see when an answer was given in millionaire tv show. I got the answer there on how to do it properly but I also managed to "do it" in this way which raised some questions I couldn't answer totally.

As you see in the code bellow I am calling a JOptionPane.showMessageDialog() after the JButton.setBackground and right before i stall the code using

 do {                           
         }while(time+i*100>System.currentTimeMillis()); 

Ignore the robot.keypress for the time. Lets get to the point. If i didn't use the JOptionPane before the code stalling , the ui would seem frozen and the button wouldn't repaint. But calling the JOptionPane.ShowMessageDialog() gives "time" to the button to repaint. Then the code is stalled normally and I achieve the sequential color repaint. I used the robot.keypress to close the Pane and achieve the effect desired.

My Questions: First, what happens when the JOptionPane is created that allows the button to repaint ? And secondly why the robot works only before the JOptionPane is called? I tried calling after the Pane was called like one would assume it should happen , but it wouldn't work in that case.

Extra: This didn't work in a mac it seems to only work for windows. Not quite sure.

 public static void paintbutton(int bnr,boolean corr) { 
       long time;
          try {          
          Robot robot = new Robot();   
                for (int i=5;i>1;i--){               
                    b[bnr-1].setBackground(null);   
                     // Simulate a key press
                    robot.keyPress(KeyEvent.VK_SPACE);
                    robot.keyRelease(KeyEvent.VK_SPACE);
                    JOptionPane.showMessageDialog(null,"hi");                                         
                    time = System.currentTimeMillis();

                    do {                           
                     }while(time+i*100>System.currentTimeMillis()); 
                    b[bnr-1].setBackground(i==1?(corr?Color.green:Color.red):Color.yellow);  
                    // Simulate a key press
                    robot.keyPress(KeyEvent.VK_SPACE);
                    robot.keyRelease(KeyEvent.VK_SPACE);
                    JOptionPane.showMessageDialog(null,"hi");                                         
                    time = System.currentTimeMillis();                    
                    do {                           
                     }while(time+i*100>System.currentTimeMillis());                     
                }
       } catch (AWTException e) {
            System.err.println("error");
              }          
   }

To avoid confusion as to the nature of this question! The code in the state below doesn't work and I know it shouldn't. I am curious on how adding JOptionPane solves that.

public static void paintbutton(int bnr,boolean corr) { 
           long time;              
                    for (int i=5;i>1;i--){               
                        b[bnr-1].setBackground(null);                                                                                     
                        time = System.currentTimeMillis();                        
                        do {                           
                         }while(time+i*100>System.currentTimeMillis()); 

                        b[bnr-1].setBackground(i==1?(corr?Color.green:Color.red):Color.yellow);                                                            
                        time = System.currentTimeMillis();                    
                        do {                           
                         }while(time+i*100>System.currentTimeMillis());                     
                    }                  
       }
Mark Booth
  • 7,605
  • 2
  • 68
  • 92
  • 1
    Quick Answer: use a Swing Timer. More detailed answer: read up on concurrency in Swing (Google those exact terms). – Hovercraft Full Of Eels Jan 14 '16 at 20:56
  • Please read the question. I know how to do this correctly. I came here looking for more insight on how this works – Dimos Fotiadis Jan 14 '16 at 21:19
  • I already received an answer on how to do this correctly actually :P – Dimos Fotiadis Jan 14 '16 at 21:19
  • Seriously, read the first article you will find by Googling "concurrency in Swing". – Hovercraft Full Of Eels Jan 14 '16 at 21:20
  • Yes, I understand why the UI freezes if I wait , I can't understand why JOptionPane.ShowMessage allows the UI to repaint and the wait works after that. – Dimos Fotiadis Jan 14 '16 at 21:24
  • 1
    There's a complex series of processes going on; there's the generation of the dialog window, which requires the creation of the native peer at the OS level, this tends to be done outside of the context of the EDT, with the result been posted back onto the Event Queue; there's the `JDialog` API taking over the event dispatching process, which may allow it to process any "pending" paint events that were in the queue before the dialog was realised, allowing the button to be painted before the dialog becomes visible – MadProgrammer Jan 14 '16 at 22:27

0 Answers0