This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
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
 
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 ]
 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!