Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

JTEXTFIELD or JPASSWORDFIELD

Thread view: 
ffellico@inwind.it - 16 Oct 2005 08:51 GMT
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.
Andrew Thompson - 16 Oct 2005 08:56 GMT
> 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.
Roedy Green - 16 Oct 2005 16:38 GMT
> > 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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

ffellico@inwind.it - 16 Oct 2005 20:42 GMT
Ok. I will not use SHOUT int the future.
ffellico@inwind.it - 16 Oct 2005 20:56 GMT
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.
Roland - 16 Oct 2005 23:06 GMT
> 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
[quoted text clipped - 8 lines]
>
> 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");
   }
}
Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \

Roland - 16 Oct 2005 23:17 GMT
> Hi.
>
[quoted text clipped - 7 lines]
>
> 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");
   }
}

Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \

Roedy Green - 17 Oct 2005 01:31 GMT
> jPasswordField.setEchoChar('\u0000');

be careful using the \u notation with control chars.  That is
equivalent to putting the control char 0 inline.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

ffellico@inwind.it - 17 Oct 2005 05:37 GMT
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
Roedy Green - 17 Oct 2005 01:29 GMT
>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

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.