• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to close JOptionPane Dialog box & come parent frame??

 
jyothi ve
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using JOptionPane.showMessageDialog(...) to show dialog on JFrame(i.e parent class).
I have 2 text fields(lastname, firstname) and a button on JFrame.
I have to validate the text fields for not empty when the user clicks button.
If the first field is empty, on close of dialog box the cursor should focus on to first empty text field. I don't want to test the second 'if' when the first was empty.

Here is the syntax (not exact code):
if(lastname.getText().equals("")) {
JOptionPane.showMessageDialog(...);
lastname.requestFocus();
}
if(firstname.getText().equals("")) {
JOptionPane.showMessageDialog(...);
firstname.requestFocus();
}
when firstname & lastname empty I am getting first dialog & second dialog.
How can we close the dialog window & come to parent frame after the first 'if' condition check without checking second 'if'(if lastname is empty).
Thanks
 
Rashid Ali
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you try like:
if....
{
}
else if...
{
}
instead of both if's use else. This will execute only when one of both conditions is true.
Hope it helps.
Rashid Ali
 
jyothi ve
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rashid Ali ,
I don't want to have if.. else if condtion. because what I have given in the question is example scenario but actual code need to do validation for many fields not only first name & last name.
I think you understand what I mean to say.
[ January 09, 2002: Message edited by: jyothi ve ]
 
Laudney Ren
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both JTextField and JButton inherit from JCompoent. JComponent have 4 methods that are useful to you in your problem:
public void setVerifyInputWhenFocusTarget(boolean flag)
/*Set the value to indicate whether input verifier for the current focus owner will be called before this component requests focus. The default is true. Set to false on components such as a Cancel button or a scrollbar, which should activate even if the input in the current focus owner is not "passed" by the input verifier for that component. */
public boolean getVerifyInputWhenFocusTarget()
/*Get the value that indicates whether the input verifier for the current focus owner will be called before this component requests focus.*/
public void setInputVerifier(InputVerifier inputVerifier)
/*Sets the input verifier for this component.*/
public InputVerifier getInputVerifier()
/*Returns the input verifier for this component.*/
 
Rashid Ali
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great info Laudney. Thanks for it.
Regards
Rashid Ali
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!