List of usage examples for javax.swing JPasswordField setFont
public void setFont(Font f)
From source file:org.openmrs.test.BaseContextSensitiveTest.java
/** * Utility method for obtaining username and password through Swing interface for tests. Any * tests extending the org.openmrs.BaseTest class may simply invoke this method by name. * Username and password are returned in a two-member String array. If the user aborts, null is * returned. <b> <em>Do not call for non-interactive tests, since this method will try to * render an interactive dialog box for authentication!</em></b> * //from w w w . j ava 2 s . co m * @param message string to display above username field * @return Two-member String array containing username and password, respectively, or * <code>null</code> if user aborts dialog */ public static synchronized String[] askForUsernameAndPassword(String message) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { } if (message == null || "".equals(message)) message = "Enter username/password to authenticate to OpenMRS..."; JPanel panel = new JPanel(new GridBagLayout()); JLabel usernameLabel = new JLabel("Username"); usernameLabel.setFont(font); usernameField = new JTextField(20); usernameField.setFont(font); JLabel passwordLabel = new JLabel("Password"); passwordLabel.setFont(font); JPasswordField passwordField = new JPasswordField(20); passwordField.setFont(font); panel.add(usernameLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 5, 0)); panel.add(usernameField, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); panel.add(passwordLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 5, 0)); panel.add(passwordField, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); frame = new JFrame(); Window window = new Window(frame); frame.setVisible(true); frame.setTitle("JUnit Test Credentials"); // We use a TimerTask to force focus on username, but still use // JOptionPane for model dialog TimerTask later = new TimerTask() { @Override public void run() { if (frame != null) { // bring the dialog's window to the front frame.toFront(); usernameField.grabFocus(); } } }; // try setting focus half a second from now new Timer().schedule(later, 500); // attention grabber for those people that aren't as observant TimerTask laterStill = new TimerTask() { @Override public void run() { if (frame != null) { frame.toFront(); // bring the dialog's window to the // front usernameField.grabFocus(); } } }; // if the user hasn't done anything in 10 seconds, tell them the window // is there new Timer().schedule(laterStill, 10000); // show the dialog box int response = JOptionPane.showConfirmDialog(window, panel, message, JOptionPane.OK_CANCEL_OPTION); // clear out the window so the timer doesn't screw up laterStill.cancel(); frame.setVisible(false); window.setVisible(false); frame = null; // response of 2 is the cancel button, response of -1 is the little red // X in the top right return (response == 2 || response == -1 ? null : new String[] { usernameField.getText(), String.valueOf(passwordField.getPassword()) }); }