13 Replies - 840 Views - Last Post: 13 January 2009 - 08:37 PM Rate Topic: -----

#1 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Having trouble grabbing whats inputed into a JTEXTFIELD

Post icon  Posted 13 January 2009 - 02:45 PM

Hi im creating a login for my applet and im trying to get whats inputed into a JTextFeild and pass it through my verifyUser() Method Im not to sure how to change my code to work please any info would be great

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;	

		}
	}
	}
	}




Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble grabbing whats inputed into a JTEXTFIELD

#2 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 02:47 PM

Hi im creating a login for my applet and im trying to get whats inputed into a JTextFeild and pass it through my verifyUser() Method Im not to sure how to change my code to work please any info would be great

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

Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 02:56 PM

Get the text from the JTextField with the getText() method.

ie

String myText = user.getText();

Was This Post Helpful? 1
  • +
  • -

#4 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 02:56 PM

thank you very much that makes sense lol
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 05:20 PM

If it is not

		   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)


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 05:38 PM

Please avoid duplicate postings :angry:
See you got twice the same answer from 2 different posts
One of us wasted his time answereing what has already been answered :angry:
Was This Post Helpful? 0
  • +
  • -

#7 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 06:54 PM

im trying find the error in my program but i cant seem to find it cuz no error comes up when i build it. when i run it on the web page i have created and press the submit button the incorrect message pops up and then it wont go away.


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);	
		
		
		}
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 07:05 PM

might wanna re phrase that lol the incorrect username and password message comes up like it should but it wont go away when i press ok
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 07:08 PM

These

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

Was This Post Helpful? 1
  • +
  • -

#10 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 07:09 PM

thanks pbl ill give it a shot
Was This Post Helpful? 0
  • +
  • -

#11 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 07:17 PM

ok i gave it a shot and when the user logs in and inputs the correct user it works fine but when the user inputs incorrectly then it still keeps coming up not sure what i did wrong if any one could check for a suggestion i would appreciate it thank you
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);	
		
		
		}
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 07:29 PM

This code
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
Was This Post Helpful? 0
  • +
  • -

#13 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 07:31 PM

how would i make it exit the action performed?
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

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Having trouble grabbing whats inputed into a JTEXTFIELD

Posted 13 January 2009 - 08:37 PM

View PostRoemeo, on 13 Jan, 2009 - 06:31 PM, said:

how would i make it exit the action performed?
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
So:
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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1