Java Code Examples for javax.swing.JPasswordField#setForeground()

The following examples show how to use javax.swing.JPasswordField#setForeground() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Query.java    From opt4j with MIT License 3 votes vote down vote up
/**
 * Create a single-line password box with the specified name, label, and
 * default value. To control the width of the box, call setTextWidth()
 * first. To get the value, call getCharArrayValue(). Calling
 * getStringValue() on a password entry will result in an error because it
 * is less secure to pass around passwords as Strings than as arrays of
 * characters.
 * <p>
 * The underlying class that is used to implement the password facility is
 * javax.swing.JPasswordField. For details about how to use JPasswordField,
 * see the <a href=
 * "http://java.sun.com/docs/books/tutorial/uiswing/components/passwordfield.html"
 * target="_top">Java Tutorial</a>
 * 
 * @param name
 *            The name used to identify the entry (when accessing the
 *            entry).
 * @param label
 *            The label to attach to the entry.
 * @param defaultValue
 *            Default value to appear in the entry box.
 * @param background
 *            The background color.
 * @param foreground
 *            The foreground color.
 * @since Ptolemy II 3.1
 */
public void addPassword(String name, String label, String defaultValue, Color background, Color foreground) {
	JLabel lbl = new JLabel(label + ": ");
	lbl.setBackground(_background);

	JPasswordField entryBox = new JPasswordField(defaultValue, _width);
	entryBox.setBackground(background);
	entryBox.setForeground(foreground);
	_addPair(name, lbl, entryBox, entryBox);

	// Add the listener last so that there is no notification
	// of the first value.
	entryBox.addActionListener(new QueryActionListener(name));

	// Add a listener for loss of focus. When the entry gains
	// and then loses focus, listeners are notified of an update,
	// but only if the value has changed since the last notification.
	// FIXME: Unfortunately, Java calls this listener some random
	// time after the window has been closed. It is not even a
	// a queued event when the window is closed. Thus, we have
	// a subtle bug where if you enter a value in a line, do not
	// hit return, and then click on the X to close the window,
	// the value is restored to the original, and then sometime
	// later, the focus is lost and the entered value becomes
	// the value of the parameter. I don't know of any workaround.
	entryBox.addFocusListener(new QueryFocusListener(name));
}