Java Dialog - Find out if OK is clicked?

↘锁芯ラ 提交于 2019-11-27 09:19:53

I'd suggest to use showConfirmDialog instead

int result = JOptionPane.showConfirmDialog(myParent, "Narrative", 
       "Title", JOptionPane.INFORMATION_MESSAGE);

and there you can test for various returns value from JDialog/JOptionPane

if (result == JOptionPane.OK_OPTION, 
              JOptionPane.CANCEL_OPTION, 
              JOptionPane.CLOSED_OPTION, etc..

I'd suggest creating a JPanel and using JOptionpane.showConfirmDialog() (or even JOptionPane.showOptionDialog() to display the dialog and retrieve the option. Here's a modification of your code as an example:

import java.awt.*;
import javax.swing.*;

class ClientDialog {
    private JTextField ip = new JTextField(20);
    private JTextField port = new JTextField(20);
    private JOptionPane optionPane;
    private JPanel panel;

    public void CreateDialog(){

        panel = new JPanel(new GridLayout(2, 2));

        panel.add(new JLabel("IP"));
        panel.add(ip);
        panel.add(new JLabel("Port"));
        panel.add(port);

        int option = JOptionPane.showConfirmDialog(null, panel, "Client Dialog", JOptionPane.DEFAULT_OPTION);

        if (option == JOptionPane.OK_OPTION) {
            System.out.println("OK!"); // do something
        }
    }

}

public class Test {

   public static void main(String args[])
   {
      ClientDialog c = new ClientDialog();
      c.CreateDialog();
   }

}

Damn, took too long to post. Anyway just design the layout of the JPanel as you would for any other sort of frame.

Please understand that the 2nd parameter in a JOptionPane, the object parameter, can be any Swing component including a JPanel that holds a simple or complex GUI.

Consider creating a JPanel, placing a few components in it including JLabels and two JTextFields, one for IP, one for port, and then displaying this JPanel in a JOptionPane. Then you can easily check if OK has been pressed and act accordingly.

import javax.swing.*;

public class OptionEg {
   public static void main(String[] args) {
      final JTextField ipField = new JTextField(10);
      final JTextField portField = new JTextField(10);
      JPanel panel = new JPanel();
      panel.add(new JLabel("IP:"));
      panel.add(ipField);

      panel.add(Box.createHorizontalStrut(15));
      panel.add(new JLabel("Port:"));
      panel.add(portField);

      int result = JOptionPane.showConfirmDialog(null, panel,
            "Enter Information", JOptionPane.OK_CANCEL_OPTION);

      if (result == JOptionPane.OK_OPTION) {
         System.out.println("IP: " + ipField.getText());
         System.out.println("Port: " + portField.getText());
      }

   }
}

Good solutions. If you want not to use something else, this is a working example: 1) set DO_NOTHING_ON_CLOSE to ensure user MUST press OK 2) verify if the JDialog is still visible.

dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.setModal(false);
dialog.setVisible(true);
dialog.setAlwaysOnTop(true);
while(dialog.isVisible())try{Thread.sleep(50);}
catch(InterruptedException e){}

This can be a solution if you want to implement a non-modal dialog box.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!