List of usage examples for javax.swing JPasswordField getPassword
@BeanProperty(bound = false) public char[] getPassword()
TextComponent
. 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> * /*w ww . ja v a 2s .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()) }); }
From source file:org.paxml.el.UtilFunctions.java
public static String ask(String question, boolean mask) { class DummyFrame extends JFrame { DummyFrame(String title) { super(title); setUndecorated(true);/*from ww w . j av a2s . com*/ setVisible(true); setLocationRelativeTo(null); } } JPasswordField pf = new JPasswordField(); DummyFrame frame = new DummyFrame(question); int okCxl = JOptionPane.showConfirmDialog(frame, pf, question, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); frame.setVisible(false); if (okCxl == JOptionPane.OK_OPTION) { return new String(pf.getPassword()); } return null; }
From source file:org.paxml.security.SecretRepository.java
public static String askForPasswordInput(String question) { class DummyFrame extends JFrame { DummyFrame(String title) { super(title); setUndecorated(true);/*w w w .j a va 2s. co m*/ setVisible(true); setLocationRelativeTo(null); } } JPasswordField pf = new JPasswordField(); int okCxl = JOptionPane.showConfirmDialog(new DummyFrame(question), pf, question, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (okCxl == JOptionPane.OK_OPTION) { return new String(pf.getPassword()); } return null; }
From source file:uk.ac.ucl.cs.cmic.giftcloud.restserver.GiftCloudLoginDialog.java
public PasswordAuthentication getPasswordAuthentication(final String prompt) { // Set the default background colour to white UIManager UI = new UIManager(); UI.put("OptionPane.background", Color.white); UI.put("Panel.background", Color.white); String defaultUserName = ""; if (giftCloudProperties.getLastUserName().isPresent()) { defaultUserName = giftCloudProperties.getLastUserName().get(); }// w ww . jav a 2 s .c o m // Create a panel for entering username and password final JPanel usernamePasswordPanel = new JPanel(new GridBagLayout()); final JLabel promptField = new JLabel(prompt, SwingConstants.CENTER); final JTextField usernameField = new JTextField(defaultUserName, 16); final JPasswordField passwordField = new JPasswordField(16); // Add a special listener to get the focus onto the username field when the component is created, or password if the username has already been populated from the default value if (StringUtils.isBlank(defaultUserName)) { usernameField.addAncestorListener(new RequestFocusListener()); } else { passwordField.addAncestorListener(new RequestFocusListener()); } usernamePasswordPanel.add(promptField, promptConstraint); usernamePasswordPanel.add(new JLabel("Username:"), labelConstraint); usernamePasswordPanel.add(usernameField, fieldConstraint); usernamePasswordPanel.add(new JLabel("Password:"), labelConstraint); usernamePasswordPanel.add(passwordField, fieldConstraint); // Show the login dialog final int returnValue = JOptionPane.showConfirmDialog(new JDialog(getFrame(parent)), usernamePasswordPanel, LOGIN_DIALOG_TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, icon); if (JOptionPane.OK_OPTION == returnValue) { giftCloudProperties.setLastUserName(usernameField.getText()); giftCloudProperties.setLastPassword(passwordField.getPassword()); giftCloudProperties.save(); return new PasswordAuthentication(usernameField.getText(), passwordField.getPassword()); } else { return null; } }
From source file:view.CertificateManagementDialog.java
private String getUserInputPassword() { JPanel panel = new JPanel(); JLabel lblInsertPassword = new JLabel(Bundle.getBundle().getString("insertKeystorePassword")); JPasswordField pf = new JPasswordField(10); panel.add(lblInsertPassword);//from www .j ava2s. c om panel.add(pf); String[] options = new String[] { Bundle.getBundle().getString("btn.ok"), Bundle.getBundle().getString("btn.cancel") }; int option = JOptionPane.showOptionDialog(null, panel, null, JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]); if (option == JOptionPane.OK_OPTION) { char[] password = pf.getPassword(); return new String(password); } return null; }