Leo Bi
Leo Bi

Reputation: 39

Return Value Issue when introduce Swing invokeLater

I am working on a java applet application, and my goal is to do something or not in a method determined by the user's choice in another pop-up window like a confirmDialogWindow. Now I am experiencing an issue that the hot keys in the pop-up window are not working.

I understand that it's because opening a pop-up window is an UI update thread, I tried things like invokeLater and invokeAndWait to order the events, the hot keys work. But my method is depending on the return value from this pop-up window to determine which control flow to go , I cannot get the proper return value if it is in invokeLater method.

How can I fix this issue?

public class SimpleDialogFrame extends JFrame {
    // Using a standard Java icon
    private Icon optionIcon = UIManager.getIcon("FileView.computerIcon");

    // Application start point
    public static void main(String[] args) {

         JFrame frame = new JFrame("Sample frame");
            frame.setSize(400, 400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            int confirmNo =0 ;
        // Use the event dispatch thread for Swing components
        // EventQueue.invokeLater(new Runnable()
        // {
        // public void run()
        // {
        // create GUI frame
            confirmNo = JOptionPane.showConfirmDialog(frame,
                    "Is this comfirmed?", "This is the dialog title",
                    JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION);
        // }
        // });
        if (confirmNo == 0) {
            // do something
            System.out.println("Clicked OK button");

        }
       else
        {
          // do something
        }
    }


}

Upvotes: 0

Views: 217

Answers (0)

Related Questions