Results 1 to 2 of 2

Thread: JDialog

  1. #1
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default JDialog

    How can I make the following JDialog run the getAccountID() mehod when it closes (disposes)? I want to be able to call it from another class in the following way (Or something similar):

    Java Code:
    int id = NewAccountDialog();
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package sciencepiction;
    
    import javax.swing.JDialog;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author johan
     */
    public class NewAccountDialog extends JDialog{
    
        public NewAccountDialog(){
            initComponents();
            pack();
            setVisible(true);
            this.setModal(true);
        }
    
        private void initComponents() {
    
            NameLabel = new javax.swing.JLabel();
            NameTextField = new javax.swing.JTextField();
            PasswordLabel = new javax.swing.JLabel();
            PasswordField = new javax.swing.JPasswordField();
            ConfirmLabel = new javax.swing.JLabel();
            ConfirmField = new javax.swing.JPasswordField();
            GradeLabel = new javax.swing.JLabel();
            GradeComboBox = new javax.swing.JComboBox();
            ConfirmButtom = new javax.swing.JButton();
            CancelButton = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Create New Account");
            setAlwaysOnTop(true);
            setResizable(false);
    
            NameLabel.setText("Account Name:");
    
            NameTextField.setText("Account Name");
            NameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    NameTextFieldFocusGained(evt);
                }
                public void focusLost(java.awt.event.FocusEvent evt) {
                    NameTextFieldFocusLost(evt);
                }
            });
    
            PasswordLabel.setText("Account Password:");
    
            PasswordField.setText("PasswordFieldStandard");
            PasswordField.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    PasswordFieldFocusGained(evt);
                }
                public void focusLost(java.awt.event.FocusEvent evt) {
                    PasswordFieldFocusLost(evt);
                }
            });
    
            ConfirmLabel.setText("Confirm Password:");
    
            ConfirmField.setText("PasswordFieldStandard");
            ConfirmField.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    ConfirmFieldFocusGained(evt);
                }
                public void focusLost(java.awt.event.FocusEvent evt) {
                    ConfirmFieldFocusLost(evt);
                }
            });
    
            GradeLabel.setText("Grade Level:");
    
            GradeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Grade 10", "Grade 11", "Grade 12" }));
    
            ConfirmButtom.setText("Confirm");
            ConfirmButtom.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    ConfirmButtomMouseClicked(evt);
                }
            });
    
            CancelButton.setText("Cancel");
            CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    CancelButtonMouseClicked(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(NameLabel)
                                .addComponent(PasswordLabel))
                            .addGap(146, 146, 146))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addComponent(NameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addComponent(PasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(ConfirmField, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))
                                .addComponent(ConfirmLabel)))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(GradeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(GradeLabel)))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(ConfirmButtom, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(CancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(NameLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(NameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(PasswordLabel)
                    .addGap(4, 4, 4)
                    .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(ConfirmLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(ConfirmField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(GradeLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(GradeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(ConfirmButtom)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(CancelButton)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }
    
        private void NameTextFieldFocusGained(java.awt.event.FocusEvent evt) {
            NameTextField.selectAll();
        }
    
        private void PasswordFieldFocusGained(java.awt.event.FocusEvent evt) {
            PasswordField.selectAll();
        }
    
        private void ConfirmFieldFocusGained(java.awt.event.FocusEvent evt) {
            ConfirmField.selectAll();
        }
    
        private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {
            dispose();
        }
    
        private void ConfirmButtomMouseClicked(java.awt.event.MouseEvent evt) {
            if(PasswordField.getText().compareTo("PasswordFieldStandard")==0){
                JOptionPane.showMessageDialog(this,"The account password you have entered is the standard field entry.\nThis can't be used as a password.\nPlease enter a valid account password.","AccountError",JOptionPane.WARNING_MESSAGE);
            }
            else{
                communicateRankings.newAccount(NameTextField.getText(), PasswordField.getText(), (10 + GradeComboBox.getSelectedIndex()));
                ID = communicateRankings.getAccountID(NameTextField.getText());
                dispose();
            }
        }
    
        private void NameTextFieldFocusLost(java.awt.event.FocusEvent evt) {
            if(NameTextField.getText().length() >30){
                JOptionPane.showMessageDialog(this,"The account name is too long!\nThe maximum amount of characters for an account name is 30.\nPlease enter a shorter account name.","AccountError",JOptionPane.WARNING_MESSAGE);
            }
            else if(communicateRankings.countAccount(NameTextField.getText()) != 0)
            {
                JOptionPane.showMessageDialog(this, "The account name \"" + NameTextField.getText() + "\" is already in use!\nPlease enter another account name that is not in use", "AccountError", JOptionPane.WARNING_MESSAGE);
                NameTextField.setText("Account Name");
            }
        }
    
        private void PasswordFieldFocusLost(java.awt.event.FocusEvent evt) {
            if(PasswordField.getText().length() >30){
                JOptionPane.showMessageDialog(this,"The account password is too long!\nThe maximum amount of characters for an account password is 30.\nPlease enter a shorter account password.","AccountError",JOptionPane.WARNING_MESSAGE);
                PasswordField.setText("PasswordFieldStandard");
            }
            else if(PasswordField.getText().compareTo("")==0){
                JOptionPane.showMessageDialog(this,"You have entered a blank account password\nPlease enter a valid account password.","AccountError",JOptionPane.WARNING_MESSAGE);
                PasswordField.setText("PasswordFieldStandard");
            }
            ConfirmField.grabFocus();
        }
    
        private void ConfirmFieldFocusLost(java.awt.event.FocusEvent evt) {
            if(PasswordField.getText().compareTo(ConfirmField.getText())!=0){
                JOptionPane.showMessageDialog(this,"The account password and confirmation password don't match!\nPlease enter a valid confirmation password.","AccountError",JOptionPane.WARNING_MESSAGE);
                PasswordField.setText("PasswordFieldStandard");
                ConfirmField.setText("PasswordFieldStandard");
                PasswordField.grabFocus();
            }
        }
    
        public int getAccountID(){
            return ID;
        }
    
        private int ID = -1;
        private DBCommunicateRankings communicateRankings = new DBCommunicateRankings();
        private javax.swing.JButton CancelButton;
        private javax.swing.JButton ConfirmButtom;
        private javax.swing.JPasswordField ConfirmField;
        private javax.swing.JLabel ConfirmLabel;
        private javax.swing.JComboBox GradeComboBox;
        private javax.swing.JLabel GradeLabel;
        private javax.swing.JLabel NameLabel;
        private javax.swing.JTextField NameTextField;
        private javax.swing.JPasswordField PasswordField;
        private javax.swing.JLabel PasswordLabel;
    
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What is that supposed to do?
    Java Code:
    int id = NewAccountDialog();
    You're not setting an owner of your Dialog.
    The reference to your dialog won't be null after hiding/disposing it.
    The constructor shouldn't call setVisible() in your case.
    The calling code should basically look like this:

    Java Code:
    NewAccountDialog ad = new NewAccountDialog(owner);
    ad.setVisible(true); // if modal, the code will stop executing here until dialog is closed
    int id = ad.getAccountID();
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. JDialog
    By frenk_castle in forum AWT / Swing
    Replies: 3
    Last Post: 05-11-2010, 01:28 PM
  2. Close JDialog
    By sky in forum AWT / Swing
    Replies: 7
    Last Post: 11-16-2009, 07:01 PM
  3. Open jDialog problem
    By jicxicmic in forum AWT / Swing
    Replies: 2
    Last Post: 11-13-2009, 02:06 PM
  4. setLocation on a JDialog is ignored
    By ScottVal in forum AWT / Swing
    Replies: 7
    Last Post: 01-13-2009, 08:35 AM
  5. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 06:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •