Example usage for javax.swing JPasswordField getEchoChar

List of usage examples for javax.swing JPasswordField getEchoChar

Introduction

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

Prototype

public char getEchoChar() 

Source Link

Document

Returns the character to be used for echoing.

Usage

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);//  www  .  j av a  2s. c  o  m
    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  .java  2s  .c  o  m*/
 */
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;
}