List of usage examples for javax.swing JTextField addAncestorListener
public void addAncestorListener(AncestorListener listener)
listener
so that it will receive AncestorEvents
when it or any of its ancestors move or are made visible or invisible. 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 . j a v 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; } }