Satyam_jay
Satyam_jay

Reputation: 149

Opening a JDialog on the click of a button

I am trying to open a JDialog when a button in a JFrame is pressed and that dialog should contain a JTable.

Where should I create the dialog (inside the frame or should a new class be created)?

Upvotes: 2

Views: 8564

Answers (1)

Alx.
Alx.

Reputation: 168

If your dialog is rather complex use a new class for it. Do something like

public class OtherDialog extends JDialog {
  // ...
  public OtherDialog(){
    // build dialog
  } 
}

and open it in your JFrame-Button-Actionhandler like this:

protected void btnOpenotherdialogActionPerformed(ActionEvent e) {
    try {
        OtherDialog dialog = new OtherDialog();
        dialog.setModalityType(ModalityType.APPLICATION_MODAL);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Upvotes: 5

Related Questions