Button for closing a JDialog

后端 未结 5 872
[愿得一人]
[愿得一人] 2020-12-18 17:59

I want to add a button (JButton) at the bottom of a JDialog which should close the JDialog when pressed. The problem is I don\'t know what to write in the ActionListener of

相关标签:
5条回答
  • 2020-12-18 18:38
    import java.awt.event.*;
    import javax.swing.*;
    
    public class YourDialog extends JDialog implements ActionListener {
    
      JButton button;
    
      public YourDialog() {
         button = new JButton("Close");
         button.addActionListener(this);
         add(button);
         pack();
         setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e) {
          dispose();
      }
    }
    
    • close only dialolg using dispose() method parent frame not closed. reason that JVM not terminated.

    0 讨论(0)
  • 2020-12-18 18:42

    I have done this slightly differently and it serves the purpose. I have several classes. They extend JPanel, serve different purposes and use JDialogs to get users' inputs.

    A typical class would contain necessary fields, save and cancel buttons.

    public class EnterpriseGUI extends javax.swing.JPanel {...} would look like this:

    Class EnterpriseGUI has a private variable jdialog and a method to set jdialog. Also ActionEvent for 'save' button and 'cancel' button.

    private JDialog jdialog;
    public void setJDialog(JDialog jdialog) {
        this.jdialog = jdialog;
    }
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Do your stuff..
        jdialog.dispose();
    } 
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // Discard your stuff..
        jdialog.dispose();
    } 
    

    Finally, I create an instance of EnterpriseGUI from any other class as needed and embed it into JDialog.

    Class OtherClass{
    private JDialog dialog;
    private EnterpriseGUI = new enterprisegui();
        private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
            this.dialog = new JDialog(this, "My dialog", true);
            this.dialog.setResizable(false);
            enterprisegui.setJDialog(dialog);
            this.dialog.getContentPane().add(enterprisegui);
            this.dialog.pack();
    
            Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();
            this.dialog.setLocation(new Double((Size.getWidth()/2) - 
                    (dialog.getWidth()/2)).intValue(), new Double((Size.getHeight()/2) - 
                            (dialog.getHeight()/2)).intValue());
            this.dialog.setVisible(true);
    
        }  
    }
    
    0 讨论(0)
  • 2020-12-18 18:45

    In addition to other answers, you can set it as the default button for the dialog root pane:

    JButton myButton = new JButton("Close");
    myButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose(); // Or whatever else
            setVisible(false);
        }
    });
    getRootPane().setDefaultButton(myButton);
    

    That way, its ActionListener will be called whenever Enter is pressed.

    0 讨论(0)
  • 2020-12-18 18:49

    In the actionPerformed() method of ActionListener you'll want something like:

    dialog.setVisible(false);
    

    If you want to get rid of the dialog permanently (free it from memory) then you would also call:

    dialog.dispose(); 
    

    ...where dialog is the name of your dialog. If dialog is a local variable, you'll need to make it final to access it in this way (or just make sure it's "effectively final" from Java 8 onwards).

    If you're adding the button as part of a subclass of JDialog (i.e. if you've got class MyDialog extends JDialog and you're adding the action listener in MyDialog) you'll want:

    MyDialog.this.setVisible(false);
    
    0 讨论(0)
  • 2020-12-18 18:51

    You can have the ActionListener dispatch a WindowEvent.WINDOW_CLOSING, as shown here.

    0 讨论(0)
提交回复
热议问题