Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOptionPane.showMessageDialog wait until OK is clicked?

This might be a very simple thing that I'm overlooking, but I just can't seem to figure it out.

I have the following method that updates a JTable:

class TableModel extends AbstractTableModel {    
        public void updateTable() {
            try {
                // update table here
             ...
    } catch (NullPointerException npe) {
                isOpenDialog = true;
                JOptionPane.showMessageDialog(null, "No active shares found on this IP!");
                isOpenDialog = false;
            }
        }
    }

However, I don't want isOpenDialog boolean to be set to false until the OK button on the message dialog is pressed, because if a user presses enter it will activate a KeyListener event on a textfield and it triggers that entire block of code again if it's set to false.

Part of the KeyListener code is shown below:

public class KeyReleased implements KeyListener {
        ...

    @Override
    public void keyReleased(KeyEvent ke) {
        if(txtIPField.getText().matches(IPADDRESS_PATTERN)) {
            validIP = true;
        } else {
            validIP = false;
        }

        if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
            if (validIP && !isOpenDialog) {
                updateTable();
            }
        }
    }
}

Does JOptionPane.showMessageDialog() have some sort of mechanism that prevents executing the next line until the OK button is pressed? Thank you.

like image 356
swiftcode Avatar asked Jun 09 '12 22:06

swiftcode


3 Answers

The JOptionPane creates a modal dialog and so the line beyond it will by design not be called until the dialog has been dealt with (either one of the buttons have been pushed or the close menu button has been pressed).

More important, you shouldn't be using a KeyListener for this sort of thing. If you want to have a JTextField listen for press of the enter key, add an ActionListener to it.

like image 137
Hovercraft Full Of Eels Avatar answered Nov 15 '22 01:11

Hovercraft Full Of Eels


An easy work around to suite your needs is the use of showConfirmDialog(...), over showMessageDialog(), this lets you take the input from the user and then proceed likewise. Do have a look at this example program, for clarification :-)

import javax.swing.*;

public class JOptionExample
{
    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                int selection = JOptionPane.showConfirmDialog(
                                null
                        , "No active shares found on this IP!"
                        , "Selection : "
                        , JOptionPane.OK_CANCEL_OPTION
                        , JOptionPane.INFORMATION_MESSAGE);
                System.out.println("I be written" +
                     " after you close, the JOptionPane");      
                if (selection == JOptionPane.OK_OPTION)
                {
                    // Code to use when OK is PRESSED.
                    System.out.println("Selected Option is OK : " + selection);
                }
                else if (selection == JOptionPane.CANCEL_OPTION)
                {
                    // Code to use when CANCEL is PRESSED.
                    System.out.println("Selected Option Is CANCEL : " + selection);
                }
            }           
        });
    }
}
like image 31
nIcE cOw Avatar answered Nov 15 '22 00:11

nIcE cOw


You can get acces to the OK button if you create optionpanel and custom dialog. Here's an example of this kind of implementation:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author OZBORN
 */
public class TestyDialog {
    static JFrame okno;
    static JPanel panel;
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        zrobOkno();
        JButton przycisk =new JButton("Dialog");
        przycisk.setSize(200,200);
        panel.add(przycisk,BorderLayout.CENTER);
        panel.setCursor(null);
        BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        przycisk.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
                            cursorImg, new Point(0, 0), "blank cursor"));
        final JOptionPane optionPane = new JOptionPane(
                "U can close this dialog\n"
                + "by pressing ok button, close frame button or by clicking outside of the dialog box.\n"
                +"Every time there will be action defined in the windowLostFocus function"
                + "Do you understand?",
                JOptionPane.INFORMATION_MESSAGE,
                JOptionPane.DEFAULT_OPTION);

        System.out.println(optionPane.getComponentCount());
        przycisk.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                final JFrame aa=new JFrame();
                final JDialog dialog = new JDialog(aa,"Click a button",false);
                ((JButton)((JPanel)optionPane.getComponents()[1]).getComponent(0)).addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        aa.dispose();
                    }
                });
                dialog.setContentPane(optionPane);
                dialog.pack();

                dialog.addWindowFocusListener(new WindowFocusListener() {
                    @Override
                    public void windowLostFocus(WindowEvent e) {
                        System.out.println("Zamykam");        
                        aa.dispose();
                    }
                    @Override public void windowGainedFocus(WindowEvent e) {}
                });

                dialog.setVisible(true);    
            }
        });
    }
    public static void zrobOkno(){
        okno=new JFrame("Testy okno");
        okno.setLocationRelativeTo(null);
        okno.setSize(200,200);
        okno.setPreferredSize(new Dimension(200,200));
        okno.setVisible(true);
        okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel=new JPanel();
        panel.setPreferredSize(new Dimension(200,200));
        panel.setLayout(new BorderLayout());
        okno.add(panel);
    }
}
like image 41
Tomasz Kaszycki Avatar answered Nov 14 '22 23:11

Tomasz Kaszycki