2 Replies - 30279 Views - Last Post: 06 October 2008 - 10:26 AM Rate Topic: -----

#1 pritish533  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-October 08

how to create a login box in java

Post icon  Posted 06 October 2008 - 09:38 AM

can anyone tell me how to create a login box in java using swing............
when u enter d password it will go to main page...


plzzz hel me outttttt
Is This A Good Question/Topic? 0
  • +

Replies To: how to create a login box in java

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,292
  • Joined: 20-September 08

Re: how to create a login box in java

Posted 06 October 2008 - 09:51 AM

I would use a BoxLayout within a CardLayout

http://java.sun.com/...ayout/card.html

http://java.sun.com/...layout/box.html
Was This Post Helpful? 0
  • +
  • -

#27 stauffski  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 210
  • Joined: 03-November 07

Re: how to create a login box in java

Posted 06 October 2008 - 10:26 AM

Well, first off you haven't pasted any code, and you are very vague on what you are using and what you want. The principal is pretty much the same whether you are asking about a web page or an application.

Create an interface like such, if it is GUI:

+-------------------------------------------+----+----+----+
|@ Login									| __ | [ ]|  X |
+-------------------------------------------+----+----+----+
|														  |
| +--Please login to your account----+					 |
| |			 _________________	|					 |
| |   Username:[_________________]   |					 |
| |			 _________________	|					 |
| |   Password:[_________________]   |					 |
| |								  |					 |
| +----------------------------------+					 |
|														  |
+----------------------------------------------------------+



Feel free to customize however you would like, but the above is a tipical login frame. Use
JFrame loginFrame = new JFrame("Login");

to create the frame. Use a panel and add an XP boarder to it with the title "Please login to your account". Use two labels a JTextField and a JPasswordField.
//set up components
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
JTextField usernameTextField = new JTextField();
JPasswordField passwordField = new JPasswordField();

//change the echo character if desired
passwordField.setEchoChar('*');


Add those to the panel and align them accordingly

Now the interface is done, next is the hard part.

You must create a file to read from so that the passwords are remembered even after the app is closed. So devise your own syntax for your file. Perhaps something like the following.
user("Bob"){
	 userName:"Bob@dic.com";
	 password:"dummy";
}
user("Joe"){
	 userName:"JoeCookieMonster@banana.yum";
	 password:"Chocolate Chip";
}


Read the file using java.io input/output streams and buffered readers.
When reading the file, be sure to read the whole line and prevent the user from using trouble symbols. Reason for this is as follows.
//the user creates his/her account
String userName = "cathy\";girl";
String password = "love";

//god knows why anyone would want a username like such, but there are some crazy people
//so, you write her info into the user file

//::::user file:::://
user("Cathy"){
	 userName:"cathy";girl";
	 password:"love";
}
...other users
//::::end user file:::://


The next time she goes to login, the computer will scan her info and look for the begining of her username by searching for "userName:\" and the end of it by searching for "\";" after "userName:\". This will cause a problem because it will pull up "cathy" as her user name and she won't be able to login.
To avoid this, don't allow users to use any of the characters you use in the syntax of the file. There are ways to allow them to use more characters than others, such as an <HTML> format, but stick with easy.

So those are the basics.

After the data is entered by the user compare the information to what's on file. Also, if you want to store account info. Put it in the braces of the user.
user("Bob"){
	 userName:"Bob@dic.com";
	 password:"dummy";
	 thumbnailsPerPage=48;
	 language="English"
	 //etc.
}



One more thing I can think of. If you want to encrypt your file, especially the password thing, I can help you out there. Just create another thread and ask about it. I'll give you tips to get into it.

Good Luck!

This post has been edited by stauffski: 06 October 2008 - 10:39 AM

Was This Post Helpful? 1

Page 1 of 1