Example usage for javax.swing JPasswordField setEchoChar

List of usage examples for javax.swing JPasswordField setEchoChar

Introduction

In this page you can find the example usage for javax.swing JPasswordField setEchoChar.

Prototype

@BeanProperty(bound = false, visualUpdate = true, description = "character to display in place of the real characters")
public void setEchoChar(char c) 

Source Link

Document

Sets the echo character for this JPasswordField.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(2, 2));
    JLabel label = new JLabel("User Name:", SwingConstants.RIGHT);
    JLabel label2 = new JLabel("Password:", SwingConstants.RIGHT);
    JTextField userNameField = new JTextField(20);

    JPasswordField passwordField = new JPasswordField();
    passwordField.setEchoChar('#');

    frame.add(label);//from w  ww .jav  a 2 s .c o  m
    frame.add(userNameField);
    frame.add(label2);
    frame.add(passwordField);
    frame.setSize(200, 70);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JPasswordField Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box rowOne = Box.createHorizontalBox();
    rowOne.add(new JLabel("Username"));
    rowOne.add(new JTextField());
    Box rowTwo = Box.createHorizontalBox();
    rowTwo.add(new JLabel("Password"));
    JPasswordField jpassword = new JPasswordField();

    rowTwo.add(jpassword);/*from  www .java 2 s .c  om*/
    f.add(rowOne, BorderLayout.NORTH);
    f.add(rowTwo, BorderLayout.SOUTH);
    f.setSize(300, 200);
    f.setVisible(true);

    jpassword.setEchoChar('#');
    System.out.println(jpassword.getEchoChar());

}

From source file:edu.ku.brc.af.auth.UserAndMasterPasswordMgr.java

/**
 * @return/* ww  w  .  ja  va2  s .c  om*/
 */
protected String[] getUserNamePasswordKey() {
    loadAndPushResourceBundle("masterusrpwd");

    FormLayout layout = new FormLayout("p, 4dlu, p, 8px, p", "p, 2dlu, p, 2dlu, p, 16px, p, 2dlu, p, 2dlu, p");
    layout.setRowGroups(new int[][] { { 1, 3, 5 } });

    PanelBuilder pb = new PanelBuilder(layout);

    final JTextField dbUsrTxt = createTextField(30);
    final JPasswordField dbPwdTxt = createPasswordField(30);
    final JTextField usrText = createTextField(30);
    final JPasswordField pwdText = createPasswordField(30);
    final char echoChar = pwdText.getEchoChar();

    final JLabel dbUsrLbl = createI18NFormLabel("USERNAME", SwingConstants.RIGHT);
    final JLabel dbPwdLbl = createI18NFormLabel("PASSWORD", SwingConstants.RIGHT);
    final JLabel usrLbl = createI18NFormLabel("USERNAME", SwingConstants.RIGHT);
    final JLabel pwdLbl = createI18NFormLabel("PASSWORD", SwingConstants.RIGHT);

    usrText.setText(usersUserName);

    CellConstraints cc = new CellConstraints();

    int y = 1;
    pb.addSeparator(UIRegistry.getResourceString("MASTER_SEP"), cc.xyw(1, y, 5));
    y += 2;

    pb.add(dbUsrLbl, cc.xy(1, y));
    pb.add(dbUsrTxt, cc.xy(3, y));
    y += 2;

    pb.add(dbPwdLbl, cc.xy(1, y));
    pb.add(dbPwdTxt, cc.xy(3, y));
    y += 2;

    pb.addSeparator(UIRegistry.getResourceString("USER_SEP"), cc.xyw(1, y, 5));
    y += 2;

    pb.add(usrLbl, cc.xy(1, y));
    pb.add(usrText, cc.xy(3, y));
    y += 2;

    pb.add(pwdLbl, cc.xy(1, y));
    pb.add(pwdText, cc.xy(3, y));

    pb.setDefaultDialogBorder();

    final CustomDialog dlg = new CustomDialog((Frame) null, getResourceString("MASTER_INFO_TITLE"), true,
            CustomDialog.OKCANCELAPPLYHELP, pb.getPanel());
    dlg.setOkLabel(getResourceString("GENERATE_KEY"));
    dlg.setHelpContext("MASTERPWD_GEN");
    dlg.setApplyLabel(showPwdLabel);

    dlg.createUI();
    dlg.getOkBtn().setEnabled(false);

    popResourceBundle();

    DocumentListener docListener = new DocumentAdaptor() {
        @Override
        protected void changed(DocumentEvent e) {
            String dbUserStr = dbUsrTxt.getText();

            boolean enable = !dbUserStr.isEmpty() && !((JTextField) dbPwdTxt).getText().isEmpty()
                    && !usrText.getText().isEmpty() && !((JTextField) pwdText).getText().isEmpty();
            if (enable && isNotEmpty(dbUserStr) && dbUserStr.equalsIgnoreCase("root")) {
                loadAndPushResourceBundle("masterusrpwd");
                UIRegistry.showLocalizedError("MASTER_NO_ROOT");
                popResourceBundle();
                enable = false;
            }
            dlg.getOkBtn().setEnabled(enable);
        }
    };

    dbUsrTxt.getDocument().addDocumentListener(docListener);
    dbPwdTxt.getDocument().addDocumentListener(docListener);
    usrText.getDocument().addDocumentListener(docListener);
    pwdText.getDocument().addDocumentListener(docListener);

    currEcho = echoChar;

    dlg.getApplyBtn().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dlg.getApplyBtn().setText(currEcho == echoChar ? hidePwdLabel : showPwdLabel);
            currEcho = currEcho == echoChar ? 0 : echoChar;
            pwdText.setEchoChar(currEcho);
            dbPwdTxt.setEchoChar(currEcho);
        }
    });

    dlg.setVisible(true);
    if (!dlg.isCancelled()) {
        return new String[] { dbUsrTxt.getText(), ((JTextField) dbPwdTxt).getText(), usrText.getText(),
                ((JTextField) pwdText).getText() };
    }

    return null;
}