Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By SurfMan

Thread: Buttons actions on JOptionPane not always performed when clicked for the first time

  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Buttons actions on JOptionPane not always performed when clicked for the first time

    Hello.

    I have my application tested and people report that whenever they have pop-up windows (implemented by JOptionPane), it happens frequently that they need to click twice or several times on its buttons before an action is performed. This happens to me every now and then as well, although not that as frequently as for other people. The application is tested on Macs only for now, so I am not sure if this problem occurs on Windows.

    This is how two kinds of JOptionPane are implemented:

    Java Code:
    package testscreen;
    
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    
    public class TestScreen {
    
        
        JFrame frame = new JFrame("Button Demo");
        
        public TestScreen() 
        {
            JFrame f = new JFrame();
            f.setPreferredSize(new Dimension(500, 500));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            int x = 5;
            int y = 5;
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(x, y));
            f.add(panel);
            
            JButton button1 = new JButton("BUTTON");
            button1.setPreferredSize(new Dimension(100, 100));
            panel.add(button1);
            
            JButton button2 = new JButton("BUTTON");
            button2.setPreferredSize(new Dimension(100, 100));
            panel.add(button2);
            
            // Add listeners to the mouse events
            button1.addActionListener(new ActionListener() 
            {
                @Override
                public void actionPerformed(ActionEvent e) 
                {
                    System.out.println("CLICKED");
                    
                    // Pop-up message warning that the flight plan will be deleted
                    JButton btn_ok_remind = new JButton("OK. Remind later");
                    JButton btn_ok_notremind = new JButton("OK. Do not remind");
                    JButton btn_cancel = new JButton("Cancel");
                    Object[] options = {btn_cancel, btn_ok_notremind, btn_ok_remind};
    
                    // Add listeners to the mouse events
                    btn_ok_remind.addMouseListener(new MouseAdapter() 
                    {
                        @Override
                        public void mouseClicked(MouseEvent e) 
                        {
                            JOptionPane.getRootFrame().dispose();
                            
                            System.out.println("CLICKED REMIND");
    
                        }
                    });
    
                    btn_ok_notremind.addMouseListener(new MouseAdapter() 
                    {
                        @Override
                        public void mouseClicked(MouseEvent e) 
                        {
                            JOptionPane.getRootFrame().dispose();
    
                            System.out.println("CLICKED DO NOT REMIND");
                        }
                    });
    
                    btn_cancel.addMouseListener(new MouseAdapter() 
                    {
                        @Override
                        public void mouseClicked(MouseEvent e) 
                        {
                            JOptionPane.getRootFrame().dispose();
                            
                            System.out.println("CLICKED CANCEL");
                        }
                    });
    
                    String message = "Fuel can be loaded only when APU and engines\n"
                            + "are not running!";
                    
                    final JOptionPane pane = new JOptionPane(   message, 
                                                                JOptionPane.WARNING_MESSAGE, 
                                                                JOptionPane.DEFAULT_OPTION,
                                                                null,
                                                                options, options[2]
                                                            );
                    final JDialog d = pane.createDialog((JFrame)null, "BUTTON 1");
                    d.setLocationRelativeTo(f);
                    d.setVisible(true);
                }
            });
            
            
            // Add listeners to the mouse events
            button2.addActionListener(new ActionListener() 
            {
                @Override
                public void actionPerformed(ActionEvent e) 
                {
                    JOptionPane.showMessageDialog(f, "BUTTON 2", "BUTTON 2",JOptionPane.ERROR_MESSAGE);
                }
            });
            
    
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
             
        }
        
        public static void main(String[] args) {
            new TestScreen();
        }
        
    }

    I have made some research and it appears that sometimes this problem may occur when certain components (e.g., JPanel) are not in focus, so I have to add action listener to every component. But what should I do with JOptionPane?

    Thanks a lot!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: Buttons actions on JOptionPane not always performed when clicked for the first ti

    You have a lot of unnecessary code. Don't create your buttons in your action listeners. Create them all outside of that. The dialog too.
    Then use the buttons to control when the dialog is displayed or hidden. The buttons displayed in the dialog can also be used to do what they
    were intended. Note that if the dialog is hidden, the buttons will still be active but since they won't be seen it is not a problem. No need to keep
    building and tearing down objects that always do the same thing.

    Finally, you do not need any mouse listeners.

    Regards,
    Jim
    igorland likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Buttons actions on JOptionPane not always performed when clicked for the first ti

    Thank you, Jim. I will rework the code. One question. What did you mean when you said:

    Quote Originally Posted by jim829 View Post
    Finally, you do not need any mouse listeners.
    Should I replace them with action listeners? How do I control what is happening as a result of clicking the JOptionPane buttons?

    Cheers.

    Igor

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,965
    Rep Power
    7

    Default Re: Buttons actions on JOptionPane not always performed when clicked for the first ti

    Quote Originally Posted by igorland View Post
    Thank you, Jim. I will rework the code. One question. What did you mean when you said:



    Should I replace them with action listeners? How do I control what is happening as a result of clicking the JOptionPane buttons?

    Cheers.

    Igor
    The method showOptionDialog in JOptionPane returns an int to show show which button was clicked. It has a built-in capability to handle custom buttons. See https://docs.oracle.com/javase/tutor...og.html#button
    Last edited by SurfMan; 05-07-2018 at 01:58 PM.
    igorland likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Buttons actions on JOptionPane not always performed when clicked for the first ti

    Yes, it works. Thanks a lot, guys, for your help!

Similar Threads

  1. Replies: 1
    Last Post: 08-13-2015, 09:04 PM
  2. Buttons being clicked in certain order in GUI
    By gfunk03 in forum AWT / Swing
    Replies: 8
    Last Post: 06-15-2011, 05:22 AM
  3. Eclipse menuitem actions on buttons
    By kanupriya.chadha in forum Eclipse
    Replies: 0
    Last Post: 06-10-2011, 06:47 AM
  4. actions for buttons
    By steel in forum AWT / Swing
    Replies: 1
    Last Post: 12-02-2010, 05:42 AM
  5. Help using Buttons/Actions with Drawings
    By Deathmonger in forum New To Java
    Replies: 0
    Last Post: 04-08-2008, 03:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •