Kenneh
Kenneh

Reputation: 33

TextField and Password field not being detected

Need to help knowing the reason why the textfield and password field is not being detected? So this is where I see the error (the red lines on txtUsername and PW)

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(txtUsername.getText().length()==0) {
        JOptionPane.showMessageDialog(null, "Please fill up blank field/s!");
    }
    else if(PW.getPassword().length==0) {
        JOptionPane.showMessageDialog(null, "Please fill up blank field/s!");
    }
    else {
        String user = txtUsername.getText();
        char[] pass = PW.getPassword();
        String pwd = String.copyValueOf(pass);
        if(validate_login(user,pwd))
            JOptionPane.showMessageDialog(null, "Correct Login Credentials!");
        else 
            JOptionPane.showMessageDialog(null,"Incorrect Login Credentials!");
    }
}

Before i put this code:

private boolean validate_login (String username, String password) {
    try {
            Class.forName(".com.mysql.jdbc.Driver");
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/logins" + "user=root&password=");
            PreparedStatement pst = con.prepareStatement("Select * from admin where username=? and password=?");
            pst.setString(1, username);
            pst.setString(2, password);
            ResultSet rs = pst.executeQuery();
            if(rs.next()) {
                return true;
            }
            else
                return false;
    }
      catch(SQLException e) {
        e.printStackTrace();
        return false;
    }    

    }
}

It doesnt have any error. Now its giving me lots of errors since i've put that code but that method alone doesnt have errors in it. Can you please help me? Thank you.

Upvotes: 0

Views: 103

Answers (2)

Kenneh
Kenneh

Reputation: 33

public class Login extends javax.swing.JFrame {

public Login() {
    initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    txtUsername = new javax.swing.JTextField();
    PW = new javax.swing.JPasswordField();
    btnCancel = new javax.swing.JButton();
    btnLogin = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setBackground(new java.awt.Color(0, 204, 153));
    jPanel1.setForeground(new java.awt.Color(255, 0, 0));

    jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Password: ");

    jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
    jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel2.setText("Username: ");

    txtUsername.setFont(new java.awt.Font("Trebuchet MS", 0, 12)); // NOI18N
    txtUsername.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtUsernameActionPerformed(evt);
        }
    });

    btnCancel.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
    btnCancel.setText("Cancel");
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCancelActionPerformed(evt);
        }
    });

    btnLogin.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
    btnLogin.setText("Login");
    btnLogin.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnLoginActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(27, 27, 27)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(btnLogin)
                            .addGap(61, 61, 61)
                            .addComponent(btnCancel))
                        .addComponent(PW)))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(txtUsername)))
            .addContainerGap(32, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(91, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(PW, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnCancel)
                .addComponent(btnLogin))
            .addGap(91, 91, 91))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(txtUsername.getText().length()==0) {
        JOptionPane.showMessageDialog(null, "Please fill up blank field/s!");
    }
    else if(PW.getPassword().length==0) {
        JOptionPane.showMessageDialog(null, "Please fill up blank field/s!");
    }
    else {
        String user = txtUsername.getText();
        char[] pass = PW.getPassword();
        String pwd = String.copyValueOf(pass);
        if(validate_login(user,pwd))
            JOptionPane.showMessageDialog(null, "Correct Login Credentials!");
        else 
            JOptionPane.showMessageDialog(null,"Incorrect Login Credentials!");
    }
}

Upvotes: 0

PeerNet
PeerNet

Reputation: 855

In your validate_login method, there is unnecessary curly brace. Remove the bottom brace

Upvotes: 3

Related Questions