I'm trying to create a custom dialog that acts much like th JOptionPane.showInputDialog() except it uses an asterisk as an echo character. So far, I've come up with the following. . .
class CustomInputDialog extends JDialog implements ActionListener
{
JLabel prompt;
JPasswordField input;
JButton ok;
JDialog dialog;
String word;
CustomInputDialog(String title, String directions)
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1));
prompt = new JLabel(directions);
input = new JPasswordField();
input.setEchoChar('*');
ok = new JButton("OK");
ok.addActionListener(this);
panel.add(prompt);
panel.add(input);
panel.add(ok);
dialog = new JDialog();
dialog.setResizable(false);
dialog.getContentPane().add(panel);
dialog.setTitle(title);
dialog.getContentPane().setLayout(new FlowLayout());
dialog.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
char[] temp = input.getPassword();
StringBuffer temp2 = new StringBuffer("");
for(int i = 0; i < temp.length; i++)
{
temp2.append(temp[i]);
}
word = temp2.toString();
}
public String getText()
{
return word;
}
}
It doesn't seem to do anything when I call it, not that I really understand how to call it. . .
I tried using CustomInputDialog d = new CustomInputDialog();
and then String aString = d.getText();
but this didn't work at all. Can someone please point me in the right direction?