Creating Authentication Window in Swing

Create Login Window in Swing


Creating a login page, if login is successful, then proceed to the next frame. This is the most given homework i think so, because many people have been asking this since time. Fine, now i've done it. This program that i've written don't just look big and not just limited to the specified homework (authentication), instead there are many things to learn from it.



What else is coded? What still can i know?


1. Using HTML tags in Swing, here i've used the <a> tag.
2. The next one was adding components (other than menus) to JMenuBar.
3. Sharing a common adapter class among the components
4. Catching an enter key and doing the action (what the users do most without clicking on the button to login).
5. Disposing a frame, instead of making it invisible or terminating the entire program.

Begin the code!

UserLogin

Welcome Frame


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class UserLogin extends JFrame
{
JButton login,cancel;
JTextField uname;
JPasswordField pass;
JLabel u,p;
public UserLogin()
{
setTitle("Login");
setLayout(new GridLayout(3,2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

u=new JLabel("Username");
p=new JLabel("Password");

uname=new JTextField(20);
pass=new JPasswordField(20);

login=new JButton("Login");
cancel=new JButton("Cancel");

add(u);
add(uname);

add(p);
add(pass);

add(login);
add(cancel);

uname.requestFocus();

cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});

login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
String un=uname.getText();
String pa=new String(pass.getPassword());

if((un.equals("myuser"))&&(pa.equals("mypass")))
{
dispose();
new WelcomeFrame();
}
}
});

KeyAdapter k=new KeyAdapter(){
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode()==KeyEvent.VK_ENTER)
login.doClick();
}
};

pass.addKeyListener(k);
uname.addKeyListener(k);

pack();
setLocationRelativeTo(null);
}

class WelcomeFrame extends JFrame
{
JLabel exit;
JMenuBar mbar;
JLabel label;

public WelcomeFrame()
{
setTitle("Welcome");
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

mbar=new JMenuBar();
mbar.setLayout(new FlowLayout());

label=new JLabel("You are logged in.");
mbar.add(label);
exit=new JLabel("<html><a href=''>Exit</a></html>");
exit.setCursor(new Cursor(Cursor.HAND_CURSOR));
exit.setFont(new Font("Tahoma",Font.PLAIN,13));

exit.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent ae)
{
System.exit(0);
}
});

mbar.add(exit);

setJMenuBar(mbar);

setExtendedState(MAXIMIZED_BOTH);
}
}

public static void main(String args[])
{
new UserLogin();
}

}

Explanation


javax.swing.*: JFrame, JLabel, JTextField, JPasswordField, JMenuBar, JButton used here.

java.awt.*: GridLayout, FlowLayout, Cursor, Font used here.

java.awt.event.*: ActionEvent, ActionListener, KeyListener, KeyEvent used here.

uname.requestFocus(): This method makes the component uname (here) focused when the frame appears.

cancel.addActionListener(....){System.exit(0);}: When cancel is clicked exit the program.

un=uname.getText(): Get what the user types in the field uname

pa=new String(pass.getPassword()): getPassword() in JPasswordField returns char[] so using the constructor of the java.lang.String class String(char[] c), i've used it.

equals(): Compares strings whether they are equal or not. Here, un, pa are compared with myuser, mypass respectively.

dispose(): If they (username,password) become true, dispose the UserLogin frame.

new WelcomeFrame(): Create WelcomeFrame object.

What if user presses enter?


k=new KeyAdapter(){}: Provide the body for the interface by providing body for the method(s) in it. Here only keyPressed(KeyEvent) is used since this is an adapter class and not directly listener interface, so it's anonymous inner class is written and it's reference is stored in k

ke.getKeyCode(): Returns the code of what key user presses.

KeyEvent.VK_ENTER: This is a static int variable containing the code for ENTER

login.doClick(): Does a click on the button login therefore, it's action event is generated and things will be done.


pack(): Pack everything. Show only the components and not the extra space.


setLocationRelativeTo(null): Center the JFrame.


WelcomeFrame


If you want to use HTML in Swing, you must use it in the form of text. As in the above example,

exit=new JLabel("<html><a href=''>Exit</a></html>"): This code creates a new label with text Exit which looks like an <a> (anchor link in your browser). Note that, the href attribute, that doesn't work here, it is of no use here, instead it is kept to appear Exit as an anchor link.

setCursor(new Cursor(Cursor.HAND_CURSOR)): This method sets the cursor for a particular component on which it is called. This method takes java.awt.Cursor as parameter. This class consists of some predefined int constants which represent several cursors. In order to create a Cursor object we need to call it's constructor by specifying the int value which represents the type of the cursor for which the Cursor object has to be created.

exit.addMouseListener(){System.exit(0);}: Exit the program when mouse is clicked on Exit (exit).

mbar.add(exit): mbar was a JMenuBar object. It was previously created but was not explained, it is undetsood and you might have seen in many programs, here i am adding exit to menubar instead of JFrame. This mbar has FlowLayout.

No comments: