javaswingjoptionpaneinvokelater

JOptionPane doesn't disappear until next JOptionPane appears


I have a process that shows confirm messages from JOptionPane. This process is called from SwingUtilities.invokeLater(runnable) that is inside an Actionlistener for a JMenuItem. The code for the runnable is this:

SwingUtilities.invokeLater(new Runnable(){
                    public void run(){
                        MyClass c=new MyClass(file)
                        try {
                            c.start();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            this.finalize();
                        } catch (Throwable e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });

In MyClass there's this method:

private boolean userInput(){
    String message="yes or no?";
    JCheckBox checkbox = new JCheckBox("Do this for all.");
    Object[] params={message,checkbox};
    int n=JOptionPane.showConfirmDialog(null,params,"message",JOptionPane.YES_NO_OPTION);
    boolean answer=(n==JOptionPane.YES_OPTION)?true:false;
    if(checkbox.isSelected()){
        nextQ=false;
        nextA=answer;
    }
    return answer;
}

which is called many times. When a JOptionPane message is displayed, I click its button(yes/no), but the message won't disappear until the next message is displayed. What might be the problem? Does this has to do with the method invokeLater ?


Solution

  • I found the way to solve this. I had to create a new Thread inside the SwingUrtilities.invokeLater method. This is the new code:

    SwingUtilities.invokeLater(new Runnable(){
                    public void run(){
                        Thread t=new Thread(new Runnable(){
                            MyClass c=new MyClass(file)
                            public void run(){
                              c.start(); 
                            }
                         }); 
                         t.start();      
                    }
                });