JTEXTFIELD or JPASSWORDFIELD

F

ffellico

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.
 
A

Andrew Thompson

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.
 
F

ffellico

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.
 
R

Roland

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_/ /__/
/ \ /_/ / \
 
R

Roland

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_/ /__/
/ \ /_/ / \
 
F

ffellico

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top