Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JTEXTFIELD or JPASSWORDFIELD

Reply
Thread Tools

JTEXTFIELD or JPASSWORDFIELD

 
 
ffellico@inwind.it
Guest
Posts: n/a
 
      10-16-2005
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.

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-16-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-16-2005
On Sun, 16 Oct 2005 07:56:29 GMT, Andrew Thompson
<(E-Mail Removed)> 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.
 
Reply With Quote
 
ffellico@inwind.it
Guest
Posts: n/a
 
      10-16-2005
Ok. I will not use SHOUT int the future.

 
Reply With Quote
 
ffellico@inwind.it
Guest
Posts: n/a
 
      10-16-2005
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.

 
Reply With Quote
 
Roland
Guest
Posts: n/a
 
      10-16-2005
On 16-10-2005 21:56, (E-Mail Removed) 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().getDocu ment());
}
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_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
Roland
Guest
Posts: n/a
 
      10-16-2005
On 16-10-2005 9:51, (E-Mail Removed) 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_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-17-2005
On 16 Oct 2005 00:51:34 -0700, (E-Mail Removed) 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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-17-2005
On Mon, 17 Oct 2005 00:17:50 +0200, Roland <(E-Mail Removed)> 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.
 
Reply With Quote
 
ffellico@inwind.it
Guest
Posts: n/a
 
      10-17-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: subclass JPasswordField markspace Java 9 07-02-2009 11:03 AM
Anybody know how to get a JOptionPane input dialog to use JPasswordField? Knute Johnson Java 3 09-30-2006 07:58 AM
JPasswordField: getPassword return type obhiee@my-deja.com Java 1 06-18-2006 02:44 PM
JPasswordField setText depreciation? terry Java 4 09-28-2004 06:41 AM
JPasswordField, charset and char[] to byte[] Kai Schlamp Java 0 04-13-2004 10:26 PM



Advertisments