Java sample(Message Dialog)


run error message 1 erroe message 2

---(Source List)---

//Example17
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Example17 {
  private static final String btnNumericCheck_code = "Integer Check";
  private static final String btnNumberRangeCheck_title = "1 - 5";
  private static final String btnNumberRangeCheck_code = "1 - 5 Check";
  private static final String window_title = "Check Number and Message Dialog";
  private static final int locateX = 10;
  private static final int locateY = 10;
  private static final int window_width = 640;
  private static final int window_height = 400;
  private static final int tf01_size = 5;
  private static final int tf02_size = 2;
  JTextField tf01 = new JTextField("", Example17.tf01_size);
  JTextField tf02 = new JTextField("", Example17.tf02_size);
  // main
  public static void main(final String ar[]) {
    final Example17 sample = new Example17();
  }
  // constructor
  public Example17() {
    // make frame
    final JFrame f = new JFrame(Example17.window_title);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // add Buttons
    final JButton btnNumberCheck = new JButton(
        Example17.btnNumericCheck_code);
    btnNumberCheck.setActionCommand(Example17.btnNumericCheck_code);
    btnNumberCheck.addActionListener(new PushButtonActionListener(f));
    final JButton btnNumberRangeCheck = new JButton(
        Example17.btnNumberRangeCheck_title);
    btnNumberRangeCheck
        .setActionCommand(Example17.btnNumberRangeCheck_code);
    btnNumberRangeCheck.addActionListener(new PushButtonActionListener(f));
    // layout Buttons and TextFields
    final JPanel p1 = new JPanel();
    p1.add(btnNumberCheck);
    p1.add(btnNumberRangeCheck);
    f.getContentPane().add(p1, BorderLayout.NORTH);
    final JPanel p2 = new JPanel();
    p2.add(this.tf01);
    p2.add(this.tf02);
    f.getContentPane().add(p2, BorderLayout.CENTER);
    // show frame
    f.setLocation(Example17.locateX, Example17.locateY);
    f.setSize(Example17.window_width, Example17.window_height);
    f.setVisible(true);
  }
  // actonListener at pushbutton
  private class PushButtonActionListener implements ActionListener {
    private static final String message01 = "Please input number!";
    private static final String message02 = "Please input 1 - 5!";
    private static final String ErrorDialog_title = "Error Message";
    JFrame f = null;
    public PushButtonActionListener(final JFrame af) {
      this.f = af;
    }
    public void actionPerformed(final ActionEvent ae) {
      final String strnum = "0123456789";
      final String str1to5 = "12345";
      if (ae.getActionCommand() == Example17.btnNumericCheck_code) {
        final String str = Example17.this.tf01.getText();
        if (str.length() == 0) {
          final ErrDialog dia = new ErrDialog(this.f,
              PushButtonActionListener.ErrorDialog_title,
              PushButtonActionListener.message01);
          return;
        }
        for (int i = 0; i < str.length(); i++) {
          if (strnum.indexOf(str.substring(i, i + 1)) == -1) {
            final ErrDialog dia = new ErrDialog(this.f,
                PushButtonActionListener.ErrorDialog_title,
                PushButtonActionListener.message01);
            return;
          }
        }
      }
      if (ae.getActionCommand() == Example17.btnNumberRangeCheck_code) {
        final String str = Example17.this.tf02.getText();
        if (str.length() != 1) {
          final ErrDialog dia = new ErrDialog(this.f,
              PushButtonActionListener.ErrorDialog_title,
              PushButtonActionListener.message02);
          return;
        }
        if (str1to5.indexOf(str) == -1) {
          final ErrDialog dia = new ErrDialog(this.f,
              PushButtonActionListener.ErrorDialog_title,
              PushButtonActionListener.message02);
          return;
        }
      }
    }
  }
  // dialog
  class ErrDialog extends JDialog {
    private static final int dialog_locateX = 10;
    private static final int dialog_locateY = 10;
    private static final int dialog_width = 200;
    private static final int dialog_height = 100;
    private static final long serialVersionUID = 1L;
    ErrDialog(final Frame f, final String title, final String msg) {
      // make modal dialog
      final JDialog dia = new JDialog(f, title, true);
      // add label
      final JLabel lab = new JLabel(msg);
      dia.getContentPane().add(BorderLayout.NORTH, lab);
      // show modal dialog
      dia.setLocation(ErrDialog.dialog_locateX, ErrDialog.dialog_locateY);
      dia.setSize(ErrDialog.dialog_width, ErrDialog.dialog_height);
      dia.setVisible(true);
    }
  }
}