chenwei
asked on
password dialog
I am going to generate a password dialog which has a password textfield a "ok" and a "cancel" buttons and will be called from an action in the caller calss. The password dialog must be modal. If one just click the "cancel" button, the dialog window will be disposed and do nothing. But if one click the "Ok" button, the entered password will be read first and then the dialog window will be disposed.
How can I do that?
How can I do that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What I want is something like JOptionPane.showInputDialo g(). The difference is by showInputDialog() one can see the entered text, but what I want is entered text should not be seen.
The actual password is stored in the original frame. That is why the password is set in the
dialog... not gotten..
I'll do a quick working example
dialog... not gotten..
I'll do a quick working example
This works for me....
I used JPasswordField and set the echo char to be '*'.
I get a depreciated warning for getText() on JPasswordText()...
let me check into what it should be...
ill append a comment in a few minutes
//in file MyDialog.java
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.border. *;
public class MyDialog extends JDialog {
JPasswordField password_field;
JButton okbutton, cancelbutton;
PasswordInterface pi;
MyDialog(PasswordInterface pi) {
super((JFrame)pi,true);
this.pi=pi;
JPanel pane= new JPanel();
this.getContentPane().add( pane);
pane.setLayout(new FlowLayout(FlowLayout.CENT ER));
password_field= new JPasswordField(15);
password_field.setEchoChar ('*');
ButtonListener buttonlistener= new ButtonListener();
okbutton= new JButton("OK");
okbutton.setActionCommand( "ok");
okbutton.addActionListener (buttonlis tener);
cancelbutton= new JButton("Cancel");
cancelbutton.setActionComm and("cance l");
cancelbutton.addActionList ener(butto nlistener) ;
pane.add(password_field);
pane.add(okbutton);
pane.add(cancelbutton);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEven t e) {
if (e.getActionCommand().equa ls("cancel ")) { MyDialog.this.dispose(); }
else if (e.getActionCommand().equa ls("ok")) {
pi.setPassword(password_fi eld.getTex t());
MyDialog.this.dispose();
}
}
}
}
//in file MyFrame.java
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class MyFrame extends JFrame implements PasswordInterface {
String stored_password;
static final MyFrame frame= new MyFrame();
JTextField field1, field2;
JButton gobutton;
public MyFrame() {
getContentPane().setLayout (new FlowLayout(FlowLayout.CENT ER));
field1= new JTextField();
field1.setColumns(10);
getContentPane().add(field 1);
ButtonListener buttonlistener= new ButtonListener();
gobutton = new JButton("GO");
gobutton.addActionListener (buttonlis tener);
getContentPane().add(gobut ton);
}
// methods for Password interface -- taken from PasswordInterface interface
public String getPassword() {
return stored_password;
}
public void setPassword(String password_from_dialog) {
stored_password= password_from_dialog;
}
public static void main(String args[]) {
//frame= new MyFrame();
frame.addWindowListener(ne w WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(800,600);
frame.setVisible(true);
}
public void testPassword() {
field1.setText(new String(stored_password));
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEven t e) {
MyDialog dialog= new MyDialog((PasswordInterfac e)frame);
dialog.setSize(new Dimension(500,400));
dialog.setLocationRelative To(frame);
dialog.setVisible(true);
// After Dialog is closed this code is executed
testPassword();
}
}
}
//in file PasswordInterface.java
public interface PasswordInterface {
public String getPassword();
public void setPassword(String password);
}
I used JPasswordField and set the echo char to be '*'.
I get a depreciated warning for getText() on JPasswordText()...
let me check into what it should be...
ill append a comment in a few minutes
//in file MyDialog.java
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.border.
public class MyDialog extends JDialog {
JPasswordField password_field;
JButton okbutton, cancelbutton;
PasswordInterface pi;
MyDialog(PasswordInterface
super((JFrame)pi,true);
this.pi=pi;
JPanel pane= new JPanel();
this.getContentPane().add(
pane.setLayout(new FlowLayout(FlowLayout.CENT
password_field= new JPasswordField(15);
password_field.setEchoChar
ButtonListener buttonlistener= new ButtonListener();
okbutton= new JButton("OK");
okbutton.setActionCommand(
okbutton.addActionListener
cancelbutton= new JButton("Cancel");
cancelbutton.setActionComm
cancelbutton.addActionList
pane.add(password_field);
pane.add(okbutton);
pane.add(cancelbutton);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEven
if (e.getActionCommand().equa
else if (e.getActionCommand().equa
pi.setPassword(password_fi
MyDialog.this.dispose();
}
}
}
}
//in file MyFrame.java
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class MyFrame extends JFrame implements PasswordInterface {
String stored_password;
static final MyFrame frame= new MyFrame();
JTextField field1, field2;
JButton gobutton;
public MyFrame() {
getContentPane().setLayout
field1= new JTextField();
field1.setColumns(10);
getContentPane().add(field
ButtonListener buttonlistener= new ButtonListener();
gobutton = new JButton("GO");
gobutton.addActionListener
getContentPane().add(gobut
}
// methods for Password interface -- taken from PasswordInterface interface
public String getPassword() {
return stored_password;
}
public void setPassword(String password_from_dialog) {
stored_password= password_from_dialog;
}
public static void main(String args[]) {
//frame= new MyFrame();
frame.addWindowListener(ne
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(800,600);
frame.setVisible(true);
}
public void testPassword() {
field1.setText(new String(stored_password));
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEven
MyDialog dialog= new MyDialog((PasswordInterfac
dialog.setSize(new Dimension(500,400));
dialog.setLocationRelative
dialog.setVisible(true);
// After Dialog is closed this code is executed
testPassword();
}
}
}
//in file PasswordInterface.java
public interface PasswordInterface {
public String getPassword();
public void setPassword(String password);
}
use getPassword() instead of getText() to get rid of the depreciation warning
The above program works on my system...
it comes up with a frame that has a textfield and a go button
click the go button
a password dialog comes up
with a JPasswordField and two buttons , ok and cancel
type in a password and hit ok and the password will appear in the original frame
instead of displaying it you probably want to check the value entered with a predetermined
password value
I displayed it so you would know what is stored
The above program works on my system...
it comes up with a frame that has a textfield and a go button
click the go button
a password dialog comes up
with a JPasswordField and two buttons , ok and cancel
type in a password and hit ok and the password will appear in the original frame
instead of displaying it you probably want to check the value entered with a predetermined
password value
I displayed it so you would know what is stored
ASKER
You program runs by me. But there is some problem: as I click the "go" button, the password dialog is shown. But if I click the "cancel" button, you can see there is a lot message output by the dos window. It menas something runs not so perfect. Have you observated?
hmm seems to work ok on my system
if cancel is selected im just calling dispose() on the dialog....
what is the first line of the message?
something like NullPointerException or something else?
if cancel is selected im just calling dispose() on the dialog....
what is the first line of the message?
something like NullPointerException or something else?
ASKER
The output message is so long that I can't see the first line. But here are some lines of message:
.......
DefaultButtonModel.fireAct ionPerform ed(Compile d Code)
DefaultButtonModel.setPres sed(Defaul tButtonMod el.java:25 0)
.......
Do you understand what these mean?
.......
DefaultButtonModel.fireAct
DefaultButtonModel.setPres
.......
Do you understand what these mean?
its basically going down all the steps where the error occurred..
a HUGE list of classes is pretty normal when dealing with Swing
classes...
A typical runtime error goes as follows:
line 1: gives the exception that was thrown (eg. NullPointerException, ArrayIndexOutOfBounds)
line 2: gives the first place the error is thrown.. it will say what class and what method the
exception was thrown.
line 3+: the exception propagates up the chain becoming more general. when an exception is thrown it be thrown in the calling class and so on and so forth... So code with a long history (a lot of calling methods) will give you a very long error list.
The first two lines are usually the ones you want to look at though (although not always).
You can get the line number that caused the problem in parenthesis.
If it says "Compiled Code" (like the line above) that means that you have the JIT compiler enabled. You can turn it off while debugging if necessary to get the line numbers.
So basically I would need the first couple lines to determine the error. I didnt test my sample code very well. I just made sure it worked for what we were talking about.
--- after a minute or two ---
Ack! I must have never actually pressed the cancel button...
whats wrong is that the variable that gets set (stored_password) is never initialized to anything
initialize it as " " or something and you wont get that message
the exception was a NullPointerException on the String
a HUGE list of classes is pretty normal when dealing with Swing
classes...
A typical runtime error goes as follows:
line 1: gives the exception that was thrown (eg. NullPointerException, ArrayIndexOutOfBounds)
line 2: gives the first place the error is thrown.. it will say what class and what method the
exception was thrown.
line 3+: the exception propagates up the chain becoming more general. when an exception is thrown it be thrown in the calling class and so on and so forth... So code with a long history (a lot of calling methods) will give you a very long error list.
The first two lines are usually the ones you want to look at though (although not always).
You can get the line number that caused the problem in parenthesis.
If it says "Compiled Code" (like the line above) that means that you have the JIT compiler enabled. You can turn it off while debugging if necessary to get the line numbers.
So basically I would need the first couple lines to determine the error. I didnt test my sample code very well. I just made sure it worked for what we were talking about.
--- after a minute or two ---
Ack! I must have never actually pressed the cancel button...
whats wrong is that the variable that gets set (stored_password) is never initialized to anything
initialize it as " " or something and you wont get that message
the exception was a NullPointerException on the String
ASKER
o.k., thank you very much.
ASKER
Could you please so kind to give me the real world example?