Example #1
0
  private void initialize(final Component customComponent, final Validator validator) {
    getRootPane().setDefaultButton(okCancelPanel.getButton("OK"));
    this.customComponent = customComponent;
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(customComponent, BorderLayout.CENTER);
    okCancelPanel.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (okCancelPanel.wasOKPressed() && validator != null) {
              String errorMessage = validator.validateInput(customComponent);
              if (errorMessage != null) {
                JOptionPane.showMessageDialog(
                    OKCancelDialog.this, errorMessage, getTitle(), JOptionPane.ERROR_MESSAGE);
                return;
              }
            }
            setVisible(false);
          }
        });

    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            okCancelPanel.setOKPressed(false);
          }
        });

    getContentPane().add(okCancelPanel, BorderLayout.SOUTH);
    pack();
    // Don't centre dialog until its size has been determined
    // i.e. after calling #pack [Jon Aquino 2005-03-09]
    GUIUtil.centreOnWindow(this);
  }