This is the password window. You must add a password field to enter password textBox.
This is the code:
package cocoland;
import Connection.DB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
* @author Amila
*/
public class passWord extends javax.swing.JFrame {
/** Creates new form passWord */
public passWord() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
passWord = new javax.swing.JPasswordField();
userName = new javax.swing.JTextField();
login = new javax.swing.JButton();
cancel = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Log in");
setResizable(false);
jLabel1.setText("User Name :");
jLabel2.setText("Password :");
passWord.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
passWordActionPerformed(evt);
}
});
login.setText("Log in");
login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loginActionPerformed(evt);
}
});
cancel.setText("Cancel");
cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(passWord, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
.addComponent(userName, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addContainerGap(42, Short.MAX_VALUE)
.addComponent(cancel)
.addGap(31, 31, 31)
.addComponent(login)))
.addGap(51, 51, 51))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancel, login});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(userName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(passWord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(login)
.addComponent(cancel))
.addContainerGap(24, Short.MAX_VALUE))
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-266)/2, (screenSize.height-142)/2, 266, 142);
}// </editor-fold>
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
try {
Connection con = DB.myCon();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from user where userName='"+userName.getText().trim()+"'and password = '"+new String(passWord.getPassword())+"'");
if (rs.first()){
mainWindow m = new mainWindow();
m.setVisible(true);
this.dispose();
JOptionPane.showMessageDialog(null, "Successfully logged on to the System, Welcome "+userName.getText(),"Welcome",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"Invalid 'UserName' or 'Password'.", "Error", JOptionPane.ERROR_MESSAGE);
userName.setText(null);
passWord.setText(null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void passWordActionPerformed(java.awt.event.ActionEvent evt) {
loginActionPerformed(evt);
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new passWord().setVisible(true);
}
});
}
// Variables declaration
private javax.swing.JButton cancel;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JButton login;
private javax.swing.JPasswordField passWord;
private javax.swing.JTextField userName;
// End of variables declaration
}
