Xperiaz X
Xperiaz X

Reputation: 226

Waiting on text field input

I guess I must explain what I am trying to do first I created a From with text field for accepting a name and another for password. Input must be stored in two strings username in UID and password in pass.

How can we store the values in the strings until the program ends and only then print it? No matter wherein main() I put the print statement, null values are printed even before the form opens.

Here is my code, generated by Netbeans. Check the main method.

package mypackage;

public class test extends javax.swing.JFrame {
    public test() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        username = new javax.swing.JTextField();
        password = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LOGIN");

        username.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usernameActionPerformed(evt);
            }
        });

        password.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordActionPerformed(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()
                .addGap(107, 107, 107)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                .addComponent(password))
                .addContainerGap(110, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(117, 117, 117)
                .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(111, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void usernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usernameActionPerformed
        uid=username.getText(); 
    }//GEN-LAST:event_usernameActionPerformed

    private void passwordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordActionPerformed
        pass=password.getText();
    }//GEN-LAST:event_passwordActionPerformed

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new test().setVisible(true);
            }
        });
        System.out.println(uid+" "+pass);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField password;
    private javax.swing.JTextField username;
    // End of variables declaration//GEN-END:variables
    static String uid,pass;
}

Upvotes: 1

Views: 941

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Use a modal JDialog or a JOptionPane for this use.

See How to Use Modality in Dialogs for more info. on how to use them to block the progress of the app. until they are closed.

Other tips

1)

username = new javax.swing.JTextField();

Should be:

// suggest a size in letters..
username = new javax.swing.JTextField(12);

2)

password = new javax.swing.JTextField();

Should be:

// use a field specialized for this purpose
password = new javax.swing.JPasswordField();

3)

Don't extend frame or other top level containers. Instead create & use an instance of one.

Upvotes: 4

Salih Erikci
Salih Erikci

Reputation: 5087

You should add a windowListener to your frame and override windowClosing.

Here is the modified version of the init that works.

private void initComponents() {
    this.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                    uid=username.getText();
                    pass=password.getText();


                System.out.println(uid+" "+pass);

        }
    });

    username = new javax.swing.JTextField();
    password = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("LOGIN");

    username.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            usernameActionPerformed(evt);
        }
    });

    password.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            passwordActionPerformed(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()
            .addGap(107, 107, 107)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                .addComponent(password))
            .addContainerGap(110, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(117, 117, 117)
            .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(28, 28, 28)
            .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(111, Short.MAX_VALUE))
    );

    pack();
}

Upvotes: 0

Devolus
Devolus

Reputation: 22084

The ActionListener from a JTextField is only performed if you press Enter while the field has the focus.

Of course you can call getText() anytime to retrieve what the textfield contains at the moment.

If you use for example, Tab to switch between the inputfields you don't get the listener fired.

If you want to make sure that you get the password when the user closes the window without having pressed enter, then you could add a WindowListener and read the values when the window closes. Or you could add a FocusListener to your textfield, so that you get also notified if the user leaves it.

Upvotes: 1

Related Questions