0

I'm currently working on a java application and I just made a login frame. It works fine but since I have to login everytime when I want to acces the second JFrame it occured to me that it is kind of irritating that you can't log in by pressing enter on the password text field. Is there a way to make the textfield use the same actionlistner as the button?

Here is the code I'm currently using. Feel free to use it for your own log in system!

package presentation;

/**
 *
 * @author Jessie den Ridder
 */


import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import presentation.ScreenInfoFrame;

public class MyLogin {

private JFrame frame = new JFrame("Login");
private final JLabel inputLabel1 = new JLabel("Gebruikersnaam");
private final JLabel inputLabel2 = new JLabel("Wachtwoord");
private JTextField input1 = new JTextField();
private JPasswordField input2 = new JPasswordField();
private final JButton button = new JButton("Login");
private final JLabel inputLabel3 = new JLabel("");

public MyLogin() {

    inputLabel1.setBounds(850, 405, 180, 20);
    input1.setBounds(1000, 400, 180, 30);

    inputLabel2.setBounds(850, 455, 180, 20);
    input2.setBounds(1000, 450, 180, 30);

    button.setBounds(1000, 520, 180, 30);
    frame.getContentPane().add(button);
    frame.getContentPane().add(inputLabel1);
    frame.getContentPane().add(input1);
    frame.getContentPane().add(input2);
    frame.getContentPane().add(inputLabel2);
    frame.getContentPane().add(inputLabel3);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setVisible(true);

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            String sql = "SELECT id, userName, password, firstName, lastName FROM employee ;";
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = (Connection) DriverManager.getConnection(
                        "Database", "user", "Password");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                String user = input1.getText();

                String pwd = (input2.getText());
                while (rs.next()) {
                    String uname = rs.getString("userName");
                    //Username is the coloumn name in the database table 
                    String password = rs.getString("password");
                    if ((user.equals(uname)) && (pwd.equals(password))) {
                        frame.dispose();
                        ScreenInfoFrame ui = new ScreenInfoFrame();
                        ui.setVisible(true);
                    }
                }
            } catch (ClassNotFoundException | SQLException k) {
                 JOptionPane.showMessageDialog(null, k.getMessage());
            }

        }
    });

}

}

  • 1
    `input1.addActionListener(..);` It will fire when the user presses the enter key. General tips: 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Jun 10 '16 at 13:58
  • ... 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 4) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 10 '16 at 13:59

2 Answers2

3

Instead of adding a new action listener to the button, create an action listener

ActionListener myActionListener = new ActionListener() {
    // Action listener body here
}

and then add that to the elements with button.addActionListener(myActionListener); and input2.AddActionListener(myActionListener);.

Addendum:

As a side note, I'd advise against giving your components generic names like button and input# as they make it difficult to discern what they do. Opt for more specific names like passwordField or submitButton.

Community
  • 1
  • 1
1

You need to create the ActionListener separately and then add it to both the text field and button:

JTextField textField = new JTextField();
JButton button = new JButton();

ActionListener actionListener = e -> {};

textField.addActionListener(actionListener);
button.addActionListener(actionListener);
explv
  • 2,709
  • 10
  • 17