hpchong7
asked on
JDialog
Dear sir,
I've created a program (using swing)that when user click
Reg->Login ,a JDialog box will be shown,and his username and password are asked for enter.However,after the dialog box is poped up,if the user switch to other application and then return to my application,the dialog box disapper and the application is hang(I think it is due to the dialog box is modal.However,for my application,it have to be modal)So,how can I do such that when I switch back to my application,user can continue entering?Thanks!
Here are the codes
.....
JFrame frame = new JFrame("LOGIN");
CustomDialog customDialog =new CustomDialog(frame);
......
if (e.getActionCommand()=="Lo g In")
{customDialog.pack();
customDialog.setLocationRe lativeTo(f rame);
customDialog.setVisible(tr ue);
String user = customDialog.getValidatedT ext();
if (user != null) {......
************************** *****
class CustomDialog extends JDialog {
private String user = null;
private String pass = null;
private String magicWord;
private JOptionPane optionPane;
public String getValidatedText() {
return user;
}
public CustomDialog(Frame aFrame) {
setTitle("LOGIN");
final String msgString1 = "Username";
final String msgString2 = "Password";
final JTextField textField = new JTextField(10);
final JPasswordField passwordfield = new JPasswordField(10);
Object[] array = {msgString1, textField,msgString2,passw ordfield};
final String btnString1 = "Login";
final String btnString2 = "Cancel";
Object[] options = {btnString1, btnString2};
optionPane = new JOptionPane(array,
JOptionPane.QUESTION_MESSA GE,
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]);
setContentPane(optionPane) ;
setDefaultCloseOperation(D O_NOTHING_ ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
optionPane.setValue(new Integer(
JOptionPane.CLOSED_OPTION) );
}
});
textField.addActionListene r(new ActionListener() {
public void actionPerformed(ActionEven t e) {
optionPane.setValue(btnStr ing1);
}
});
passwordfield.addActionLis tener(new ActionListener() {
public void actionPerformed(ActionEven t e) {
optionPane.setValue(btnStr ing1);
}
});
optionPane.addPropertyChan geListener (new PropertyChangeListener() {
public void propertyChange(PropertyCha ngeEvent e) {
String prop = e.getPropertyName();
if (isVisible()
&& (e.getSource() == optionPane)
&& (prop.equals(JOptionPane.V ALUE_PROPE RTY) ||
prop.equals(JOptionPane.IN PUT_VALUE_ PROPERTY)) ) {
Object value = optionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_ VALUE) {
//ignore reset
return;
}
// Reset the JOptionPane's value.
// If you don't do this, then if the user
// presses the same button next time, no
// property change event will be fired.
optionPane.setValue(
JOptionPane.UNINITIALIZED_ VALUE);
if (value.equals(btnString1)) {
user = textField.getText();
pass = passwordfield.getText();
if (pass.equals("4180")) {
// we're done; dismiss the dialog
setVisible(false);
} else {
// text was invalid
textField.selectAll();
JOptionPane.showMessageDia log(
CustomDialog.this,
"Wrong Password",
"Try again",
JOptionPane.ERROR_MESSAGE) ;
user = null;
}
} else { // user closed dialog or clicked cancel
/*dd.setLabel("It's OK. "
+ "We won't force you to type "
+ magicWord + ".");*/
user = null;
setVisible(false);
}
}
}
});
}
}
I've created a program (using swing)that when user click
Reg->Login ,a JDialog box will be shown,and his username and password are asked for enter.However,after the dialog box is poped up,if the user switch to other application and then return to my application,the dialog box disapper and the application is hang(I think it is due to the dialog box is modal.However,for my application,it have to be modal)So,how can I do such that when I switch back to my application,user can continue entering?Thanks!
Here are the codes
.....
JFrame frame = new JFrame("LOGIN");
CustomDialog customDialog =new CustomDialog(frame);
......
if (e.getActionCommand()=="Lo
{customDialog.pack();
customDialog.setLocationRe
customDialog.setVisible(tr
String user = customDialog.getValidatedT
if (user != null) {......
**************************
class CustomDialog extends JDialog {
private String user = null;
private String pass = null;
private String magicWord;
private JOptionPane optionPane;
public String getValidatedText() {
return user;
}
public CustomDialog(Frame aFrame) {
setTitle("LOGIN");
final String msgString1 = "Username";
final String msgString2 = "Password";
final JTextField textField = new JTextField(10);
final JPasswordField passwordfield = new JPasswordField(10);
Object[] array = {msgString1, textField,msgString2,passw
final String btnString1 = "Login";
final String btnString2 = "Cancel";
Object[] options = {btnString1, btnString2};
optionPane = new JOptionPane(array,
JOptionPane.QUESTION_MESSA
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]);
setContentPane(optionPane)
setDefaultCloseOperation(D
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
optionPane.setValue(new Integer(
JOptionPane.CLOSED_OPTION)
}
});
textField.addActionListene
public void actionPerformed(ActionEven
optionPane.setValue(btnStr
}
});
passwordfield.addActionLis
public void actionPerformed(ActionEven
optionPane.setValue(btnStr
}
});
optionPane.addPropertyChan
public void propertyChange(PropertyCha
String prop = e.getPropertyName();
if (isVisible()
&& (e.getSource() == optionPane)
&& (prop.equals(JOptionPane.V
prop.equals(JOptionPane.IN
Object value = optionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_
//ignore reset
return;
}
// Reset the JOptionPane's value.
// If you don't do this, then if the user
// presses the same button next time, no
// property change event will be fired.
optionPane.setValue(
JOptionPane.UNINITIALIZED_
if (value.equals(btnString1))
user = textField.getText();
pass = passwordfield.getText();
if (pass.equals("4180")) {
// we're done; dismiss the dialog
setVisible(false);
} else {
// text was invalid
textField.selectAll();
JOptionPane.showMessageDia
CustomDialog.this,
"Wrong Password",
"Try again",
JOptionPane.ERROR_MESSAGE)
user = null;
}
} else { // user closed dialog or clicked cancel
/*dd.setLabel("It's OK. "
+ "We won't force you to type "
+ magicWord + ".");*/
user = null;
setVisible(false);
}
}
}
});
}
}
ASKER
Edited text of question.
JDialog(Frame owner, String title, boolean modal)
Creates a modal or non-modal dialog with the specified title and the specified owner frame.
....So make this your first line in your CustomDialog class constructor :
super(aFrame,"LOGIN",true) ;
Creates a modal or non-modal dialog with the specified title and the specified owner frame.
....So make this your first line in your CustomDialog class constructor :
super(aFrame,"LOGIN",true)
ASKER
I am sorry that it fails.Problem still exists.
I think the dialog box should have its icon under the window toolbar when runs,but it did not.
I think the dialog box should have its icon under the window toolbar when runs,but it did not.
ovi is absolutely rigth ,but in a more detailed way.
add lines shown by //***
public CustomDialog(Frame aFrame)
{
//***
super(aFrame,"LOGIN",true) ;
//***
setTitle("LOGIN");
final String msgString1 = "Username";
final String msgString2 = "Password";
//more code
}
for confirmation see
http://forum.java.sun.com/forum?13@68.8qI3aXa5c2A^0@.ee9c042/0
add lines shown by //***
public CustomDialog(Frame aFrame)
{
//***
super(aFrame,"LOGIN",true)
//***
setTitle("LOGIN");
final String msgString1 = "Username";
final String msgString2 = "Password";
//more code
}
for confirmation see
http://forum.java.sun.com/forum?13@68.8qI3aXa5c2A^0@.ee9c042/0
ASKER
Thank you very much,i've understanded more!
However ,actually my parent application is a JApplet.So when I call the constructor,should I write
super(japplet,"Login",true )
?
//but the complier complains.....
However ,actually my parent application is a JApplet.So when I call the constructor,should I write
super(japplet,"Login",true
?
//but the complier complains.....
JOptionPane.createDialog(C omponent parentComponent,String title)
???
each JOptionPane dialog is modal
see http://manning.spindoczine.com/sbe/files/uts2/Chapter14html/Chapter14.htm
???
each JOptionPane dialog is modal
see http://manning.spindoczine.com/sbe/files/uts2/Chapter14html/Chapter14.htm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much!
However....where should I add?In the class CustomDialog below the line setContentPane(...)?
However....where should I add?In the class CustomDialog below the line setContentPane(...)?
Dialog is a Temporary Window for displaying information or requesting keystrokes. It requires a parent Frame, thus it cannot be used inside an Applet which has no Frame. It can be modal, which means it blocks input to all other Windows until it is dismissed.
ASKER
thnak you!
ASKER