Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
Textfields are single line areas that can be used to enter in data from the keyboard while a program is running. To create a text field use any of the constructors:
TextField() TextField(int columns) // where columns gives the TextField a width TextField(String s) TextField(String s, int columns)
To allow a user to enter a password or any other hidden text use:
setEchoChar(char c) // normally char c is '*' boolean echoCharIsSet()
To make the Textfield editable or un-editable use:
setEditable(boolean editable) // true means editable
TextField Example: (Run Applet)
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class TextFieldDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements DocumentListener { private final JTextField text; private final JLabel message = new JLabel(""); public View() { super(); this.setLayout(new BorderLayout()); this.text = new JTextField(20); // 20 characters wide this.add("North", this.text); this.add("South", this.message); // Add Document listener for the document that is attached // to the JTextField final Document document = this.text.getDocument(); document.addDocumentListener(this); this.text.requestFocus(); } @Override public void insertUpdate(DocumentEvent e) { showText(); } @Override public void removeUpdate(DocumentEvent e) { showText(); } @Override public void changedUpdate(DocumentEvent e) { showText(); } private void showText() { this.message.setText("Text input is: " + this.text.getText()); } } }
Passwords are TextFields that have their text hidden.
PasswordDemo Example: (Run Applet)
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class PasswordDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements DocumentListener { // Password private final JPasswordField password = new JPasswordField(20); private final JLabel message = new JLabel(); public View() { super(); // add a document listener to the password field final Document passwordDocument; passwordDocument = this.password.getDocument(); passwordDocument.addDocumentListener(this); // applet Panel layout this.setLayout(new BorderLayout()); this.add("North", this.password); this.add("South", this.message); } // Document Listener overrides @Override public void insertUpdate(DocumentEvent e) { showMessage(); } @Override public void removeUpdate(DocumentEvent e) { showMessage(); } @Override public void changedUpdate(DocumentEvent e) { showMessage(); } private void showMessage() { this.message.setText(new String(this.password.getPassword())); } } }
The password echo character can be turned off by setting it to (char)0.
Password_Hint_Demo Example: (Run Applet)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Password_Hint_Demo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { private final HintPasswordField password = new HintPasswordField(20); private final HintPasswordField confirmPassword = new HintPasswordField(20); public View() { super(); this.add(this.password); this.add(this.confirmPassword); } } public class HintPasswordField extends JPasswordField implements FocusListener { public HintPasswordField(int length) { super(length); this.showHint(true); this.addFocusListener(this); } @Override public void focusGained(FocusEvent e) { final String password = new String(getPassword()); if (password.equals("Password")) { this.showHint(false); } } @Override public void focusLost(FocusEvent e) { final String password = new String(getPassword()); if (password.equals("")) { this.showHint(true); } } private void showHint(boolean showHint) { if (showHint) // sets the password field to hide text and show hint { this.setEchoChar((char) 0); this.setForeground(Color.lightGray); this.setText("Password"); } else // sets the password field to show password protected text { this.setEchoChar('*'); this.setForeground(Color.black); this.setText(""); } } } }
Password_Confirmation_Demo Example: (Run Applet)
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class Password_Confirmation_Demo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements ActionListener { private final JButton submit = new JButton("Submit"); private final HintPasswordField password = new HintPasswordField(20); private final HintPasswordField confirmPassword = new HintPasswordField(20); private final JLabel message = new JLabel(""); public View() { super(); final JLabel blank = new JLabel(""); final JLabel passwordLabel = new JLabel("Password"); final JLabel confirmPasswordLabel = new JLabel("Confirm Password"); final JPanel passwordPanel = new JPanel(); // applet Panel layout passwordPanel.setLayout(new GridLayout(3, 2)); passwordPanel.add(passwordLabel); passwordPanel.add(this.password); passwordPanel.add(confirmPasswordLabel); passwordPanel.add(this.confirmPassword); passwordPanel.add(blank); passwordPanel.add(this.submit); this.setLayout(new BorderLayout()); this.add("North", passwordPanel); this.add("South", this.message); this.submit.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { final Object source = e.getSource(); if (source == this.submit) { final char[] password1 = this.password.getPassword(); final char[] password2 = this.confirmPassword.getPassword(); if (Arrays.equals(password1, password2)) { this.message.setForeground(Color.black); this.message.setText("Match"); } else { this.message.setForeground(Color.red); this.message.setText("Different"); } // for security reasons, // set both password char arrays // to all '0' after use Arrays.fill(password1, '0'); Arrays.fill(password2, '0'); } } } public class HintPasswordField extends JPasswordField implements FocusListener { public HintPasswordField(int length) { super(length); this.showHint(true); this.addFocusListener(this); } @Override public void focusGained(FocusEvent e) { final String password = new String(getPassword()); if (password.equals("Password")) { this.showHint(false); } } @Override public void focusLost(FocusEvent e) { final String password = new String(getPassword()); if (password.equals("")) { this.showHint(true); } } private void showHint(boolean showHint) { if (showHint) // sets the password field to hide text and show hint { this.setEchoChar((char) 0); this.setForeground(Color.lightGray); this.setText("Password"); } else // sets the password field to show password protected text { this.setEchoChar('*'); this.setForeground(Color.black); this.setText(""); } } } }
Password_Strength_Demo Example: (Run Applet)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Password_Strength_Demo extends JApplet { // Define the various strengths that a password can have private static enum PasswordStrength { INVALID, WEAK, MEDIUM, STRONG; } @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements ActionListener { // Password private final HintPasswordField password = new HintPasswordField(15); private final HintPasswordField confirmPassword = new HintPasswordField(15); private final JButton submit = new JButton("Submit"); private final JLabel message = new JLabel(); public View() { super(); final JPanel passwordPanel = new JPanel(); final JPanel confirmPasswordPanel = new JPanel(); final JPanel passwordsPanel = new JPanel(); final JPanel submitPanel = new JPanel(); final JLabel enterPasswordMessage = new JLabel("Enter Password: "); final JLabel confirmPasswordMessage = new JLabel("Confirm Password: "); // this.passwordPanel layout passwordPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); passwordPanel.add(enterPasswordMessage); passwordPanel.add(this.password); // this.confirmPasswordPanel layout confirmPasswordPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); confirmPasswordPanel.add(confirmPasswordMessage); confirmPasswordPanel.add(this.confirmPassword); // this.submitPanel layout submitPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); submitPanel.add(this.submit); // this.passwordsPanel layout passwordsPanel.setLayout(new GridLayout(3, 1)); passwordsPanel.add(passwordPanel); passwordsPanel.add(confirmPasswordPanel); passwordsPanel.add(submitPanel); // main Panel layout this.setLayout(new BorderLayout()); this.add("North", passwordsPanel); this.add("South", this.message); this.submit.addActionListener(this); } // ActionListener overrides @Override public void actionPerformed(ActionEvent e) { final Object source = e.getSource(); if (source == this.submit) { this.showText(); } } private void showText() { switch (getPasswordStrength(new String(this.password.getPassword()), new String(this.confirmPassword.getPassword()))) { case INVALID: { this.showMessage(Color.red, "Passwords do not match"); break; } case WEAK: { this.showMessage(Color.red, "Weak"); break; } case MEDIUM: { this.showMessage(Color.blue, "Medium"); break; } case STRONG: { this.showMessage(Color.green, "Strong"); break; } } } private PasswordStrength getPasswordStrength(String password, String confirmedPassword) { boolean hasAlpha = false; boolean hasDecimal = false; boolean hasOtherCharacters = false; PasswordStrength passwordStrength = PasswordStrength.WEAK; if (!password.equals(confirmedPassword)) { passwordStrength = PasswordStrength.INVALID; return passwordStrength; } char c; // find out if password contains alpha, decimal or other characters for (int i = 0; i < password.length(); i++) { c = password.charAt(i); if ((c >= '0') && (c <= '9')) { hasDecimal = true; } else if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) { hasAlpha = true; } else { hasOtherCharacters = true; } } if (hasAlpha && hasDecimal) { if (hasOtherCharacters) { passwordStrength = PasswordStrength.STRONG; } else { passwordStrength = PasswordStrength.MEDIUM; } } else if (hasAlpha || hasDecimal) { passwordStrength = PasswordStrength.WEAK; } return passwordStrength; } private void showMessage(Color color, String message) { this.message.setForeground(color); this.message.setText(message); } } public class HintPasswordField extends JPasswordField implements FocusListener { public HintPasswordField(int length) { super(length); this.showHint(true); this.addFocusListener(this); } @Override public void focusGained(FocusEvent e) { final String password = new String(getPassword()); if (password.equals("Password")) { this.showHint(false); } } @Override public void focusLost(FocusEvent e) { final String password = new String(getPassword()); if (password.equals("")) { this.showHint(true); } } private void showHint(boolean showHint) { if (showHint) // sets the password field to hide text and show hint { this.setEchoChar((char) 0); this.setForeground(Color.lightGray); this.setText("Password"); } else // sets the password field to show password protected text { this.setEchoChar('*'); this.setForeground(Color.black); this.setText(""); } } } }
A text area is a multi-line area used to display text. It can be set to either allow editing or to be read-only.
TextArea example: (Run Applet)
import java.awt.*; import javax.swing.*; public class TextAreaDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { public View() { final JTextArea area1 = new JTextArea(); final JTextArea area2 = new JTextArea("We shall set this to be read only.\n"); final JLabel gap = new JLabel(""); this.setLayout(new GridLayout(1, 3)); this.add(area1); this.add(gap); this.add(area2); area2.setEditable(false); area1.append("We can insert text into read/write a text area and \nuse the '\\n' escape code to move to a new line."); area2.append("The programmer can insert text into a read only text area, but the user can not."); // replace the two letters 'ha' from // shall with this new string area2.replaceRange("NEW TEXT", 4, 6); } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.