JTEXTFIELD or JPASSWORDFIELD

Discussion in 'Java' started by ffellico@inwind.it, Oct 16, 2005.

  1. Guest

    Hi.

    I need to show in a text box alternatively it's contents in clear mode
    or as asterisks but I discover that there is no way to do this with a
    JTextField nor with a JPasswordField.

    Someone wrote in this group that in a JPasswordField it's possible to
    disable the echoed character using setEchoCar(0) but in this case I
    receive a compile error because there the argument must be a char).

    So. Someone can help me ? Thank you from Franco.
     
    , Oct 16, 2005
    #1
    1. Advertising

  2. wrote:

    > Sub: JTEXTFIELD or JPASSWORDFIELD


    Please don't SHOUT.

    > I need to show in a text box alternatively it's contents in clear mode
    > or as asterisks but I discover that there is no way to do this with a
    > JTextField nor with a JPasswordField.


    java.awt.CardLayout ..best of all worlds.
     
    Andrew Thompson, Oct 16, 2005
    #2
    1. Advertising

  3. Roedy Green Guest

    On Sun, 16 Oct 2005 07:56:29 GMT, Andrew Thompson
    <> wrote or quoted :

    > > Sub: JTEXTFIELD or JPASSWORDFIELD

    >
    >Please don't SHOUT.


    The reason to avoid upper case is that large blocks of it are hard to
    read, not that it hurts the ears.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 16, 2005
    #3
  4. Guest

    Ok. I will not use SHOUT int the future.
     
    , Oct 16, 2005
    #4
  5. Guest

    I thank you for suggestion, but this is not to easy as in other
    languages such in Delphi or C# where I can use a textbox and easily use
    a property to change the appearence of the displayed text.

    I think that you suggest me to put a jPasswordField and a jTextField as
    two cards in the CardLayout container so alternatively I can show the
    desired one, but I need that when a user change one of the two
    components, automatically the other one must be syncronized so I must
    copy the text programmatically back and forth. Yes, this is possible,
    but I think that it's not to elegant.

    Any way. Thanks very much.. Franco.
     
    , Oct 16, 2005
    #5
  6. Roland Guest

    On 16-10-2005 21:56, wrote:

    > I thank you for suggestion, but this is not to easy as in other
    > languages such in Delphi or C# where I can use a textbox and easily use
    > a property to change the appearence of the displayed text.
    >
    > I think that you suggest me to put a jPasswordField and a jTextField as
    > two cards in the CardLayout container so alternatively I can show the
    > desired one, but I need that when a user change one of the two
    > components, automatically the other one must be syncronized so I must
    > copy the text programmatically back and forth. Yes, this is possible,
    > but I think that it's not to elegant.
    >
    > Any way. Thanks very much.. Franco.
    >


    No need to synchronize yourself when you share the model (a Document
    instance) between the text field and the password field:

    jPasswordField = new JPasswordField();
    jTextField = new JTextField();
    jTextField.setDocument(jPasswordField.getDocument());

    Here's a complete example:

    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.JToggleButton;
    import java.awt.CardLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class ShareDocument extends JFrame {
    public static void main(String[] args) {
    ShareDocument app = new ShareDocument();
    app.pack();
    app.setVisible(true);
    }

    private CardLayout cardLayout;
    private JPanel jContentPane;
    private JPanel jPanel;
    private JPasswordField jPasswordField;
    private JTextField jTextField;
    private JToggleButton jToggleButton;

    public ShareDocument() {
    super();
    initialize();
    }
    private CardLayout getCardLayout() {
    if (cardLayout == null) {
    cardLayout = new CardLayout();
    }
    return cardLayout;
    }
    private JPanel getJContentPane() {
    if (jContentPane == null) {
    jContentPane = new JPanel();
    jContentPane.setLayout(new BorderLayout());
    jContentPane.add(getJToggleButton(),
    java.awt.BorderLayout.NORTH);
    jContentPane.add(getJPanel(), java.awt.BorderLayout.SOUTH);
    }
    return jContentPane;
    }
    private JPanel getJPanel() {
    if (jPanel == null) {
    jPanel = new JPanel();
    jPanel.setLayout(getCardLayout());
    jPanel.add(getJPasswordField(), getJPasswordField()
    .getName());
    jPanel.add(getJTextField(), getJTextField().getName());
    }
    return jPanel;
    }
    private JPasswordField getJPasswordField() {
    if (jPasswordField == null) {
    jPasswordField = new JPasswordField();
    jPasswordField.setName("jPasswordField");
    jPasswordField.setColumns(20);
    }
    return jPasswordField;
    }
    private JTextField getJTextField() {
    if (jTextField == null) {
    jTextField = new JTextField();
    jTextField.setName("jTextField");
    jTextField.setColumns(20);
    jTextField.setDocument(getJPasswordField().getDocument());
    }
    return jTextField;
    }
    private JToggleButton getJToggleButton() {
    if (jToggleButton == null) {
    jToggleButton = new JToggleButton();
    jToggleButton.setText("Show Text");
    jToggleButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (jToggleButton.isSelected()) {
    getCardLayout().show(getJPanel(),
    getJTextField().getName());
    jToggleButton.setText("Hide Text");
    } else {
    getCardLayout().show(getJPanel(),
    getJPasswordField().getName());
    jToggleButton.setText("Show Text");
    }
    }
    });
    }
    return jToggleButton;
    }
    private void initialize() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.setContentPane(getJContentPane());
    this.setTitle("Share Document");
    }
    }
    --
    Regards,

    Roland de Ruiter
    ` ___ ___
    `/__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Oct 16, 2005
    #6
  7. Roland Guest

    On 16-10-2005 9:51, wrote:

    > Hi.
    >
    > I need to show in a text box alternatively it's contents in clear mode
    > or as asterisks but I discover that there is no way to do this with a
    > JTextField nor with a JPasswordField.
    >
    > Someone wrote in this group that in a JPasswordField it's possible to
    > disable the echoed character using setEchoCar(0) but in this case I
    > receive a compile error because there the argument must be a char).
    >
    > So. Someone can help me ? Thank you from Franco.
    >

    Use
    jPasswordField.setEchoChar('\u0000');
    or
    jPasswordField.setEchoChar((char)0);

    Example:

    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JToggleButton;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class ShowPassword extends JFrame {
    public static void main(String[] args) {
    ShowPassword app = new ShowPassword();
    app.pack();
    app.setVisible(true);
    }

    private JPanel jContentPane;
    private JPasswordField jPasswordField;
    private JToggleButton jToggleButton;

    public ShowPassword() {
    super();
    initialize();
    }
    private JPanel getJContentPane() {
    if (jContentPane == null) {
    jContentPane = new JPanel();
    jContentPane.setLayout(new BorderLayout());
    jContentPane.add(getJToggleButton(),
    java.awt.BorderLayout.NORTH);
    jContentPane.add(getJPasswordField(), BorderLayout.SOUTH);
    }
    return jContentPane;
    }
    private JPasswordField getJPasswordField() {
    if (jPasswordField == null) {
    jPasswordField = new JPasswordField();
    jPasswordField.setColumns(20);
    }
    return jPasswordField;
    }
    private JToggleButton getJToggleButton() {
    if (jToggleButton == null) {
    jToggleButton = new JToggleButton();
    jToggleButton.setText("Show Password");
    jToggleButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (jToggleButton.isSelected()) {
    getJPasswordField().setEchoChar('\u0000');
    jToggleButton.setText("Hide Password");
    } else {
    getJPasswordField().setEchoChar('*');
    jToggleButton.setText("Show Password");
    }
    }
    });
    }
    return jToggleButton;
    }
    private void initialize() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.setContentPane(getJContentPane());
    this.setTitle("Show Password");
    }
    }

    --
    Regards,

    Roland de Ruiter
    ` ___ ___
    `/__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Oct 16, 2005
    #7
  8. Roedy Green Guest

    On 16 Oct 2005 00:51:34 -0700, wrote or quoted :

    >disable the echoed character using setEchoCar(0) but in this case I
    >receive a compile error because there the argument must be a char).

    You just need to cast the 0 with (char).

    = and new byte[] are smart about accepting a byte literal. Method
    parms are not.

    for sample code see http://mindprod.com/jgloss/jpasswordfield.html


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 17, 2005
    #8
  9. Roedy Green Guest

    On Mon, 17 Oct 2005 00:17:50 +0200, Roland <> wrote or
    quoted :

    > jPasswordField.setEchoChar('\u0000');


    be careful using the \u notation with control chars. That is
    equivalent to putting the control char 0 inline.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 17, 2005
    #9
  10. Guest

    Thanks for the example. That will be useful for me to use CardLayoutin
    the future, but to solve my problem it's sufficient and easy the echo
    character disable.

    So what you said with the statement:

    jPasswordField.setEchoChar((char)0)

    it's the solution. I thank you also for the Roland comments. Bye.
    Franco
     
    , Oct 17, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kai Schlamp
    Replies:
    0
    Views:
    638
    Kai Schlamp
    Apr 13, 2004
  2. terry
    Replies:
    4
    Views:
    3,584
    Paul Lutus
    Sep 28, 2004
  3. Knute Johnson
    Replies:
    3
    Views:
    4,250
    sailpra
    Sep 30, 2006
  4. Replies:
    1
    Views:
    4,162
    ddimitrov
    Jun 18, 2006
  5. markspace

    Re: subclass JPasswordField

    markspace, Jul 1, 2009, in forum: Java
    Replies:
    9
    Views:
    583
    John B. Matthews
    Jul 2, 2009
Loading...

Share This Page