Java Swing How to - Create Custom Buttons in JOptionPane.showInputDialog








Question

We would like to know how to create Custom Buttons in JOptionPane.showInputDialog.

Answer

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*w w w .ja v a2  s.c o  m*/
public class Main {

  public static void main(String[] args) {
    Object[] options1 = { "Try This Number", "Choose A Random Number", "Quit" };

    JPanel panel = new JPanel();
    panel.add(new JLabel("Enter number between 0 and 1000"));
    JTextField textField = new JTextField(10);
    panel.add(textField);

    int result = JOptionPane.showOptionDialog(null, panel, "Enter a Number",
        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
        options1, null);
    if (result == JOptionPane.YES_OPTION) {
      JOptionPane.showMessageDialog(null, textField.getText());
    }
  }
}