hello please help me on this how to make the log in password in gui that can accept at least 6 characters and not more than 10 characters..and if i will re enter the password and it will prompt to the user that password is already exist..hoping for your positive responds..thanks in advance....

Recommended Answers

All 6 Replies

have you written anything yourself?
you'll have to use Swing, and quite basic programming logic ...
show some effort and it 'll be easier for us to guide you through the rest

hello please help me on this how to make the log in password in gui that can accept at least 6 characters and not more than 10 characters..and if i will re enter the password and it will prompt to the user that password is already exist..hoping for your positive responds..thanks in advance....

ok, please show some effort, if you do not know how to get started see this example of what you asked

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

public class Frame1 extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String text = "<html> <center><b>Login</b></center> </html>";

	JPanel jPanel1 = new JPanel();
	BorderLayout borderLayout1 = new BorderLayout();
	JLabel jLabel1 = new JLabel();
	JPanel jPanel2 = new JPanel();
	GridBagLayout gridBagLayout1 = new GridBagLayout();
	JLabel jLabel2 = new JLabel();
	JLabel jLabel3 = new JLabel();
	JTextField loginTextField = new JTextField(10);
	JPasswordField passwordTextField = new JPasswordField(10);
	JPanel jPanel3 = new JPanel();
	JButton exitButton = new JButton();
	JButton enterButton = new JButton();
	FlowLayout flowLayout1 = new FlowLayout();

	public Frame1() {
		try {
			jbInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	

	private void login(ActionEvent e) {
		String login = loginTextField.getText();
		String password = new String(passwordTextField.getPassword());
		// validate login and password here. validity will be done by sending
		// login/password to the server
		if (login.equals("test") && password.equals("test")) {
			System.out.println("login successfull");
			
		} else {
			JOptionPane.showMessageDialog(this, "Incorrect login or password",
					"Error", JOptionPane.ERROR_MESSAGE);
			loginTextField.setText("");
			passwordTextField.setText("");
			loginTextField.requestFocusInWindow();
		}
	}

	

	private void exit(ActionEvent e) {
		setVisible(false);
	}

	private void jbInit() throws Exception {
		jPanel1.setLayout(borderLayout1);
		jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
		jLabel1.setText(text);
		jPanel2.setLayout(gridBagLayout1);
		jLabel2.setText("Password:");
		jLabel3.setText("User:");
		exitButton.setText("Exit");
		exitButton.addActionListener(new java.awt.event.ActionListener() {

			public void actionPerformed(ActionEvent e) {
				exit(e);
			}
		});
		enterButton.setText("Enter");
		enterButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login(e);
			}
		});
		jPanel3.setLayout(flowLayout1);
		flowLayout1.setAlignment(FlowLayout.RIGHT);
		this.getContentPane().add(jPanel1, BorderLayout.CENTER);
		jPanel1.add(jLabel1, BorderLayout.NORTH);
		jPanel1.add(jPanel2, BorderLayout.CENTER);
		jPanel2.add(loginTextField, new GridBagConstraints(2, 1, 1, 1, 0.0,
				0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
				new Insets(0, 11, 0, 0), 0, 0));
		jPanel2.add(jLabel2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						11, 0, 0, 0), 0, 0));
		jPanel2.add(passwordTextField, new GridBagConstraints(2, 2, 1, 1, 0.0,
				0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
				new Insets(11, 11, 0, 0), 0, 0));
		jPanel2.add(jLabel3, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						0, 0, 0, 0), 0, 0));
		jPanel1.add(jPanel3, BorderLayout.SOUTH);
		jPanel3.add(enterButton, null);
		jPanel3.add(exitButton, null);
	}

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		Frame1 f = new Frame1();
		f.setLocation(200, 200);
		f.pack();
		f.show();
	}
}

finally code the part where you validate the lenght of the password

/**
* Do yourself a method to evaluate the lenght of your inputLogin
 * */
private boolean validatePasswordLenght(String password){
//something
	return false;
}

also note that in the line

if (login.equals("test") && password.equals("test")) {

System.out.println("login successfull");

We are checking for the user 'test' with password 'test'

hello thanks for the reply i will write again if i have problem..more power to you

hi can you help me what is the meaning of printstacktrace and how to use it and when to use it..also the jbinit() how to use and when to use it ....hoping for your positive responds....thanks in advance..

printStackTrace() is method that helps you to print out stack of errors messages that was thrown for some reason. In these messages you can find out where was error caused and then you can investigate.

jbInit() is method to add GUI components to frame. Don't you read that code setLayout(), setText(), add()

oki thanks for the reply.more power to you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.