Java Swing How to - Assign a specific action to the "Cancel" button within JOptionPane.showInputDialog








Question

We would like to know how to assign a specific action to the "Cancel" button within JOptionPane.showInputDialog.

Answer

import java.text.ParseException;
/*from ww  w  . ja v  a2  s . c  o m*/
import javax.swing.JOptionPane;

public class Main {

  public static void main(String[] args) throws ParseException {
    String input = JOptionPane.showInputDialog("Enter Input:");
    if (input == null) {
      System.out.println("Calcel pressed");
    } else {
      System.out.println("OK pressed");
    }

  }
}