Hi all,

I'm working on a project for my CS class to go a little above and beyond the requirements. I have a simple GUI that takes in a username and password and checks a few things (needs uppercase and digit, etc), but I'm trying to have a JLabel that updates as the user types to check typed text against stored names.

If I pass just a single string to my function (called copyText()), it works great, but when I try to test in a for loop then it doesn't work.

package com.mahimahi42.gsst.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel login;
	private JLabel password;
	private JLabel check;
	private JTextField loginInput;
	private JTextField passInput;
	private JPanel user;
	private JPanel pass;
	private JPanel passCheck;
	private JButton submit;
	private LinkedList<String> names = new LinkedList<String>();
	
	public Test() {
		login = new JLabel("Username");
		password = new JLabel("Password");
		check = new JLabel();
		loginInput = new JTextField(20);
		passInput = new JTextField(20);
		submit = new JButton("Login");
		user = new JPanel();
		pass = new JPanel();
		passCheck = new JPanel();
		
		user.setLayout(new FlowLayout());
		pass.setLayout(new FlowLayout());
		passCheck.setLayout(new GridLayout(2, 1));
		
		user.add(login);
		user.add(loginInput);
		
		pass.add(password);
		pass.add(passInput);
		
		passCheck.add(pass);
		
		check.setOpaque(true);
		check.setBackground(Color.white);
		
		passCheck.add(check);
		
		populateNames();
		
		setLayout(new BorderLayout());
		
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String userText = loginInput.getText();
				String passText = passInput.getText();
				
				if (userText.equals("") && passText.equals("")) {
					JOptionPane.showMessageDialog(null, "No login nor password were entered");
				} 
				else if (passText.equals("") && !userText.equals("")) {
					JOptionPane.showMessageDialog(null, "No password was entered");
				}
				else if (userText.equals("") && !passText.equals("")) {
					JOptionPane.showMessageDialog(null, "No login was entered");
				}
				
				boolean upper = false;
				boolean digit = false;
				boolean chosen = false;
				boolean userOk = false;
				
				for (int i = 0; i < userText.length(); i++) {
					if (Character.isUpperCase(userText.charAt(i))) {
						upper = true;
					}
					if (Character.isDigit(userText.charAt(i))) {
						digit = true;
					}
				}
				if (upper && digit) {
					JOptionPane.showMessageDialog(null, "Login was properly chosen");
					for (String s : names) {
						if (s.equals(userText)) {
							chosen = true;
						}
					}
					if (chosen) {
						JOptionPane.showMessageDialog(null, "Login name already taken");
					}
					else {
						JOptionPane.showMessageDialog(null, "Login name has been created");
						names.add(userText);
						userOk = true;
					}
				}
				else {
					if (!userText.equals("")) {
						JOptionPane.showMessageDialog(null, "Login should have at least one uppercase letter and one number");
					}
				}
				
				upper = false;
				digit = false;
				
				for (int i = 0; i < passText.length(); i++) {
					if (Character.isUpperCase(passText.charAt(i))) {
						upper = true;
					}
					if (Character.isDigit(passText.charAt(i))) {
						digit = true;
					}
				}
				if (upper && digit && userOk) {
					JOptionPane.showMessageDialog(null, "Login was successful");
				}
				else if (!upper || !digit) {
					if (!passText.equals("")) {
						JOptionPane.showMessageDialog(null, "Password should have at least one uppercase letter and one number");
					}
				}
			}
		});
		
		loginInput.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent evt) {
				copyText(evt, "Mahimahi42");
			}
		});
		
		add(user, BorderLayout.NORTH);
		add(passCheck, BorderLayout.CENTER);
		add(submit, BorderLayout.SOUTH);
		
		this.pack();
		this.setSize(400, 200);
		this.setVisible(true);
	}
	
	public void populateNames() {
		names.add("Mahimahi42");
		names.add("Pwns4uce");
		names.add("Usernam3");
	}
	
	private void copyText(KeyEvent evt, String s) {
			if (loginInput.getText().equals(s)) {
				check.setText("Username taken");
				check.setBackground(Color.red);
			}
			else {
				check.setText("Username not taken");
				check.setBackground(Color.green);
			}
	}
	
	public static void main(String[] args) {
		new Test();
	}
}

Thoughts?

I doubt you need a loop for that, take a look at the api's for jTextField. I'm sure there are some listeners mentioned that can help you check your content of the field every time the value changes.

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.