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