| | |
program code about log in information using java JFrame
![]() |
Any beginner's java book has examples on how to create gui using javax.swing.If you don't have already such a book search the internet for tutorials. And check the java API for the following classes:
JFrame, JPanel, JTextField, JPasswordField, JButton, JLabel
And the interface ActionListener
JFrame, JPanel, JTextField, JPasswordField, JButton, JLabel
And the interface ActionListener
Check out my New Bike at my Public Profile at the "About Me" tab
Sample Code for Login Using JFrame with MySql Server Database as Back End
Further Discussion IM ahilansoftware@gmail.com,ahilan_23@yahoo.co.in
import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.*;
import java.sql.*;
import java.io.*;
/**
* Summary description for UserVallidation
*
*/
public class UserVallidation extends JFrame {
// Variables declaration
private JLabel jLabel1;
private JLabel jLabel2;
private JTextField jTextField1;
private JPasswordField jPasswordField1;
private JButton jButton1;
private JButton jButton2;
private JPanel contentPane;
Boolean loop = false;
// End of variables declaration
public UserVallidation() {
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//
this.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent() {
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jTextField1 = new JTextField();
jPasswordField1 = new JPasswordField();
jButton1 = new JButton();
jButton2 = new JButton();
contentPane = (JPanel)this.getContentPane();
//
// jLabel1
//
jLabel1.setText("EK UserName");
//
// jLabel2
//
jLabel2.setText("EK Password");
//
// jTextField1
jTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField1_actionPerformed(e);
}
});
//
// jPasswordField1
//
jPasswordField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jPasswordField1_actionPerformed(e);
}
});
//
// jButton1
//
jButton1.setText("OK");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
//
// jButton2
//
jButton2.setText("Cancel");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
//check boxes
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 10,20,91,18);
addComponent(contentPane, jLabel2, 10,70,77,18);
addComponent(contentPane, jTextField1, 100,20,100,22);
addComponent(contentPane, jPasswordField1, 100,70,100,22);
addComponent(contentPane, jButton1, 50,100,83,28);
addComponent(contentPane, jButton2, 170,100,83,28);
// UserVallidation
//
this.setTitle("EK User Verification");
this.setLocation(new Point(390, 300));
this.setSize(new Dimension(290, 200));
jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jPasswordField1KeyPressed(evt);
}
/* public void keyTyped(java.awt.event.KeyEvent evt) {
txt_useremailKeyTyped(evt);
}*/
});
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height) {
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTextField1_actionPerformed(ActionEvent e) {
System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jPasswordField1_actionPerformed(ActionEvent e) {
System.out.println("\njPasswordField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jButton1_actionPerformed(ActionEvent e) {
login();
System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
}
//action listener
private void jPasswordField1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if(evt.getKeyCode() == 10) {
login();
}
}
public void login() {
String username=null;
String password=null;
String dburl=null;
String dburl1=null;
String dburl2=null;
DataInputStream dis=null;
username = new String(jTextField1.getText());
password = new String(jPasswordField1.getPassword());
if((username.equals(""))&&(password.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the UserName and Password","User Verification",1);
new UserVallidation();
} else if((username.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the UserName ","User Verification",1);
new UserVallidation();
} else if((password.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the Password ","User Verification",1);
new UserVallidation();
} else {
try {
dburl = "jdbc:mysql://127.0.0.1:3306/"
dburl1 = "root";
dburl2="admin";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(dburl,dburl1,dburl2);
Statement stat=con.createStatement();
System.out.println("connection opened");
PreparedStatement pstmt1=null;
ResultSet rs=null;
do{
loop = false;
username = new String(jTextField1.getText());
password = new String(jPasswordField1.getPassword());
pstmt1=con.prepareStatement("select username,password from TBLEK_REGISTRATION where username=? and password=?");
pstmt1.setString(1,username);
pstmt1.setString(2,password);
rs=pstmt1.executeQuery();
if(!rs.next() && rs.getRow() == 0) {
JOptionPane.showMessageDialog(this,"Login Failed. Please try again!");
jTextField1.setText("");
jPasswordField1.setText("");
loop = true;
new UserVallidation();
break;
}else {
username = rs.getString("username");
password=rs.getString("password");
this.setVisible(false);
}
}while (loop);
}catch(Exception Ex) {
System.out.println("Exception arises"+Ex);
}
}
this.setEnabled(false);
this.setVisible(false);
}
private void jButton2_actionPerformed(ActionEvent e) {
System.exit(1);
System.out.println("\njButton2_actionPerformed(ActionEvent e) called.");
}
}
Further Discussion IM ahilansoftware@gmail.com,ahilan_23@yahoo.co.in
import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.*;
import java.sql.*;
import java.io.*;
/**
* Summary description for UserVallidation
*
*/
public class UserVallidation extends JFrame {
// Variables declaration
private JLabel jLabel1;
private JLabel jLabel2;
private JTextField jTextField1;
private JPasswordField jPasswordField1;
private JButton jButton1;
private JButton jButton2;
private JPanel contentPane;
Boolean loop = false;
// End of variables declaration
public UserVallidation() {
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//
this.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent() {
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jTextField1 = new JTextField();
jPasswordField1 = new JPasswordField();
jButton1 = new JButton();
jButton2 = new JButton();
contentPane = (JPanel)this.getContentPane();
//
// jLabel1
//
jLabel1.setText("EK UserName");
//
// jLabel2
//
jLabel2.setText("EK Password");
//
// jTextField1
jTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField1_actionPerformed(e);
}
});
//
// jPasswordField1
//
jPasswordField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jPasswordField1_actionPerformed(e);
}
});
//
// jButton1
//
jButton1.setText("OK");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
//
// jButton2
//
jButton2.setText("Cancel");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
//check boxes
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 10,20,91,18);
addComponent(contentPane, jLabel2, 10,70,77,18);
addComponent(contentPane, jTextField1, 100,20,100,22);
addComponent(contentPane, jPasswordField1, 100,70,100,22);
addComponent(contentPane, jButton1, 50,100,83,28);
addComponent(contentPane, jButton2, 170,100,83,28);
// UserVallidation
//
this.setTitle("EK User Verification");
this.setLocation(new Point(390, 300));
this.setSize(new Dimension(290, 200));
jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jPasswordField1KeyPressed(evt);
}
/* public void keyTyped(java.awt.event.KeyEvent evt) {
txt_useremailKeyTyped(evt);
}*/
});
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height) {
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTextField1_actionPerformed(ActionEvent e) {
System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jPasswordField1_actionPerformed(ActionEvent e) {
System.out.println("\njPasswordField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jButton1_actionPerformed(ActionEvent e) {
login();
System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
}
//action listener
private void jPasswordField1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if(evt.getKeyCode() == 10) {
login();
}
}
public void login() {
String username=null;
String password=null;
String dburl=null;
String dburl1=null;
String dburl2=null;
DataInputStream dis=null;
username = new String(jTextField1.getText());
password = new String(jPasswordField1.getPassword());
if((username.equals(""))&&(password.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the UserName and Password","User Verification",1);
new UserVallidation();
} else if((username.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the UserName ","User Verification",1);
new UserVallidation();
} else if((password.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the Password ","User Verification",1);
new UserVallidation();
} else {
try {
dburl = "jdbc:mysql://127.0.0.1:3306/"
dburl1 = "root";
dburl2="admin";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(dburl,dburl1,dburl2);
Statement stat=con.createStatement();
System.out.println("connection opened");
PreparedStatement pstmt1=null;
ResultSet rs=null;
do{
loop = false;
username = new String(jTextField1.getText());
password = new String(jPasswordField1.getPassword());
pstmt1=con.prepareStatement("select username,password from TBLEK_REGISTRATION where username=? and password=?");
pstmt1.setString(1,username);
pstmt1.setString(2,password);
rs=pstmt1.executeQuery();
if(!rs.next() && rs.getRow() == 0) {
JOptionPane.showMessageDialog(this,"Login Failed. Please try again!");
jTextField1.setText("");
jPasswordField1.setText("");
loop = true;
new UserVallidation();
break;
}else {
username = rs.getString("username");
password=rs.getString("password");
this.setVisible(false);
}
}while (loop);
}catch(Exception Ex) {
System.out.println("Exception arises"+Ex);
}
}
this.setEnabled(false);
this.setVisible(false);
}
private void jButton2_actionPerformed(ActionEvent e) {
System.exit(1);
System.out.println("\njButton2_actionPerformed(ActionEvent e) called.");
}
}
![]() |
Other Threads in the Java Forum
- Previous Thread: JSP input fields and tabpage
- Next Thread: help with sorting the array in alphabetical order
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






