import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
public class Login extends JApplet
{
private JTextField user;
private JPasswordField pass;
private String UserName;
private String PassWord;
private JButton login;
private JLabel lblstat;
static final String JDBC_Driver = "com.mysql.jdbc.Driver";
static final String DataBase_URL = "jdbc:mysql://localhost/players";
private JLabel userlbl;
private JLabel passlbl;
public void init()
{
login loginScreen = new login();
setContentPane(loginScreen);
}
class login extends JPanel implements ActionListener
{
public login()
{
setLayout(new GridLayout(4,2,5,5));
setBorder(BorderFactory.createTitledBorder("login"));
JLabel lbluser = new JLabel("Username: ");
JLabel lblpass = new JLabel("Password: ");
user = new JTextField();
userlbl = new JLabel();
userlbl.setText(user);
pass = new JPasswordField();
passlbl = new JLabel();
passlbl.setText(pass);
login = new JButton("Submit");
login.addActionListener(this);
JLabel space = new JLabel("");
}
public boolean VerifyUser(String UN, String PW)
{
boolean myFlag = false;
try{
Class.forName(JDBC_Driver);
Connection connection = null;
Statement statement = null;
connection = DriverManager.getConnection( DataBase_URL, "George", "Jess");
String sql = "Select * From customers Where userName = '"+ UN + "' AND userPass = '" + PW + "'";
Statement st = connection.createStatement();
ResultSet resultSet = st.executeQuery(sql);
connection.close();
}
catch (Exception err)
{
JOptionPane.showMessageDialog(null, err.getMessage());
}
return myFlag;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == login)
{
boolean myFlag2 = false;
boolean myFlag4 = false;
do
{
if(user == null)
{
myFlag4 = true;
}
if(pass == null)
{
myFlag4 = true;
}
if(myFlag4 != true)
{
myFlag2 = VerifyUser(userlbl, passlbl);
}
if(myFlag2 == true)
{
JOptionPane.showMessageDialog(null, "Correct UserName and Password");
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect UserName or Password");
}
}while(myFlag2 != true && myFlag4 == false);
boolean myFlag3 = false;
}
}
}
}
Having trouble grabbing whats inputed into a JTEXTFIELDDuplicate postings... topics merfed
Page 1 of 1
13 Replies - 840 Views - Last Post: 13 January 2009 - 08:37 PM
#1
Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 02:45 PM
Replies To: Having trouble grabbing whats inputed into a JTEXTFIELD
#2
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 02:47 PM
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
public class Login extends JApplet
{
private JTextField user;
private JPasswordField pass;
private JButton login;
private JLabel lblstat;
static final String JDBC_Driver = "com.mysql.jdbc.Driver";
static final String DataBase_URL = "jdbc:mysql://localhost/players";
private JLabel userlbl;
private JLabel passlbl;
public void init()
{
login loginScreen = new login();
setContentPane(loginScreen);
}
class login extends JPanel implements ActionListener
{
public login()
{
setLayout(new GridLayout(4,2,5,5));
setBorder(BorderFactory.createTitledBorder("login"));
JLabel lbluser = new JLabel("Username: ");
JLabel lblpass = new JLabel("Password: ");
user = new JTextField();
userlbl = new JLabel();
userlbl.setText(user);
pass = new JPasswordField();
passlbl = new JLabel();
passlbl.setText(pass);
login = new JButton("Submit");
login.addActionListener(this);
JLabel space = new JLabel("");
}
public boolean VerifyUser(String UN, String PW)
{
boolean myFlag = false;
try{
Class.forName(JDBC_Driver);
Connection connection = null;
Statement statement = null;
connection = DriverManager.getConnection( DataBase_URL, "George", "Jess");
String sql = "Select * From customers Where userName = '"+ UN + "' AND userPass = '" + PW + "'";
Statement st = connection.createStatement();
ResultSet resultSet = st.executeQuery(sql);
connection.close();
}
catch (Exception err)
{
JOptionPane.showMessageDialog(null, err.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == login)
{
boolean myFlag2 = false;
boolean myFlag4 = false;
do
{
if(user == null)
{
myFlag4 = true;
}
if(pass == null)
{
myFlag4 = true;
}
if(myFlag4 != true)
{
myFlag2 = VerifyUser(userlbl, passlbl);
}
if(myFlag2 == true)
{
JOptionPane.showMessageDialog(null, "Correct UserName and Password");
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect UserName or Password");
}
}while(myFlag2 != true && myFlag4 == false);
boolean myFlag3 = false;
}
}
}
}
sory didnt mean to post twice my computer was acting weird so i stopped the post cuz it would send it and i re posted it i apologize
This post has been edited by Roemeo: 13 January 2009 - 02:48 PM
#3
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 02:56 PM
ie
String myText = user.getText();
#4
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 02:56 PM
#5
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 05:20 PM
if(user == null)
Your JTextField is surely not null but you want to test the content of it which is a String for its length (trim without the spaces)
if(user.getText().trim().length() == 0)
#6
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 05:38 PM
See you got twice the same answer from 2 different posts
One of us wasted his time answereing what has already been answered
#7
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 06:54 PM
here is my code any help would be appreciated.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
public class Login extends JApplet
{
private JTextField user;
private JPasswordField pass;
private JButton login;
private JLabel lblstat;
private String UserName;
private String PassWord;
static final String JDBC_Driver = "com.mysql.jdbc.Driver";
static final String DataBase_URL = "jdbc:mysql://localhost/customers";
public void init()
{
login loginScreen = new login();
setContentPane(loginScreen);
}
class login extends JPanel implements ActionListener
{
public login()
{
setLayout(new GridLayout(4,2,5,5));
setBorder(BorderFactory.createTitledBorder("login"));
JLabel lbluser = new JLabel("Username: ");
JLabel lblpass = new JLabel("Password: ");
user = new JTextField();
UserName = user.getText();
pass = new JPasswordField();
PassWord = pass.getText();
login = new JButton("Submit");
login.addActionListener(this);
JLabel space = new JLabel("");
JLabel lblauth = new JLabel("Validation:");
lblstat = new JLabel("Failed");
add(lbluser);
add(user);
add(lblpass);
add(pass);
add(space);
add(login);
add(lblauth);
add(lblstat);
}
public boolean VerifyUser(String UN, String PW)
{
boolean myFlag = false;
try{
Class.forName(JDBC_Driver);
Connection connection = null;
Statement statement = null;
connection = DriverManager.getConnection( DataBase_URL, "George", "Jess");
String sql = "Select * From customers Where userName = '"+ UN + "' AND userPass = '" + PW + "'";
Statement st = connection.createStatement();
ResultSet resultSet = st.executeQuery(sql);
while(resultSet.next())
{
myFlag = true;
}
connection.close();
}
catch (Exception err)
{
//JOptionPane.showMessageDialog(null, err.getMessage());
}
return myFlag;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == login)
{
boolean myFlag2 = false;
boolean myFlag4 = false;
do
{
if(UserName == null)
{
myFlag4 = true;
}
if(PassWord == null)
{
myFlag4 = true;
}
if(myFlag4 != true)
{
myFlag2 = VerifyUser(UserName, PassWord);
}
if(myFlag2 == true)
{
JOptionPane.showMessageDialog(null, "correct UserName or Password");
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect UserName or Password");
}
}while(myFlag2 != true && myFlag4 == false);
}
}
}
}
#8
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 07:05 PM
#9
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 07:08 PM
UserName = user.getText(); PassWord = pass.getText();
should be done in the actionPerformed() method after the user has had the time to put something in the JTextField not in the constructor. At that moment moment both JTextField are empty.
You cannot do:
UserName = user.getText();
and expect UserName to change everytime the JTextField changes. That is not the way it works. This is a ponctual operation which copies what is in the JTextField at a precise moment.
This post has been edited by pbl: 13 January 2009 - 07:10 PM
#10
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 07:09 PM
#11
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 07:17 PM
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
public class Login extends JApplet
{
private JTextField user;
private JPasswordField pass;
private JButton login;
private JLabel lblstat;
private String UserName;
private String PassWord;
static final String JDBC_Driver = "com.mysql.jdbc.Driver";
static final String DataBase_URL = "jdbc:mysql://localhost/customers";
public void init()
{
login loginScreen = new login();
setContentPane(loginScreen);
}
class login extends JPanel implements ActionListener
{
public login()
{
setLayout(new GridLayout(4,2,5,5));
setBorder(BorderFactory.createTitledBorder("login"));
JLabel lbluser = new JLabel("Username: ");
JLabel lblpass = new JLabel("Password: ");
user = new JTextField();
pass = new JPasswordField();
login = new JButton("Submit");
login.addActionListener(this);
JLabel space = new JLabel("");
JLabel lblauth = new JLabel("Validation:");
lblstat = new JLabel("");
add(lbluser);
add(user);
add(lblpass);
add(pass);
add(space);
add(login);
add(lblauth);
add(lblstat);
}
public boolean VerifyUser(String UN, String PW)
{
boolean myFlag = false;
try{
Class.forName(JDBC_Driver);
Connection connection = null;
Statement statement = null;
connection = DriverManager.getConnection( DataBase_URL, "George", "Jess");
String sql = "Select * From customers Where userName = '"+ UN + "' AND userPass = '" + PW + "'";
Statement st = connection.createStatement();
ResultSet resultSet = st.executeQuery(sql);
while(resultSet.next())
{
myFlag = true;
}
connection.close();
}
catch (Exception err)
{
//JOptionPane.showMessageDialog(null, err.getMessage());
}
return myFlag;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == login)
{
boolean myFlag2 = false;
boolean myFlag4 = false;
UserName = user.getText();
PassWord = pass.getText();
do
{
if(UserName == null)
{
myFlag4 = true;
}
if(PassWord == null)
{
myFlag4 = true;
}
if(myFlag4 != true)
{
myFlag2 = VerifyUser(UserName, PassWord);
}
if(myFlag2 == true)
{
JOptionPane.showMessageDialog(null, "correct UserName or Password");
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect UserName or Password");
}
}while(myFlag2 != true && myFlag4 == false);
}
}
}
}
#12
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 07:29 PM
do
{
if(UserName == null)
{
myFlag4 = true;
}
if(PassWord == null)
{
myFlag4 = true;
}
if(myFlag4 != true)
{
myFlag2 = VerifyUser(UserName, PassWord);
}
if(myFlag2 == true)
{
JOptionPane.showMessageDialog(null, "correct UserName or Password");
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect UserName or Password");
}
}while(myFlag2 != true && myFlag4 == false);
will be performed while(myFlag2 != true && myFlag4 == false
and the user won't have any chance to change what are in the JTextField during that time
If the user login is correct you'll have to execute another method that display other screens or something else
if the user login is wrong, you have to exit the actionPerformed() and wait for him to change the JTextfield and pressed on the OKButton again which will call the actionPerformed again
#13
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 07:31 PM
oh and once the user gets it correct i plan to redirect to another page i jus havent had the chance to put that in yet cuz i was stuck on the if you get it wrong problem lol
This post has been edited by Roemeo: 13 January 2009 - 07:32 PM
#14
Re: Having trouble grabbing whats inputed into a JTEXTFIELD
Posted 13 January 2009 - 08:37 PM
Roemeo, on 13 Jan, 2009 - 06:31 PM, said:
oh and once the user gets it correct i plan to redirect to another page i jus havent had the chance to put that in yet cuz i was stuck on the if you get it wrong problem lol
if(login is correct)
-- redirect to another page
else
-- return
Just forget your do... while()
it will exit actionPerformed() by itself
This post has been edited by pbl: 13 January 2009 - 08:38 PM
|
|

New Topic/Question
Reply




MultiQuote




|