?
Solved

return value of a dialog box.

Posted on 2001-07-04
3
Medium Priority
?
244 Views
Last Modified: 2006-11-17
// Defines what event takes place when button is clicked
public void actionPerformed(ActionEvent e)
{

  if(name.equals("Quit"))
  {
   //Show an information panel with the options yes/no and message 'choose one':
   JOptionPane.showConfirmDialog(null,"Are you sure of that?", "RE-CONFIRM", JOptionPane.YES_NO_OPTION);
  }

}

From the above codes, how can I capture which button did the user clicked on. If he clicks on Yes then the program will end but when he clicks on No, the program will continue.
0
Comment
Question by:heuer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
mattyk earned 200 total points
ID: 6253417
The call to JOptionPane.showConfirmDialog will return an int indicating which value was selected

so something like

// Defines what event takes place when button is clicked
public void actionPerformed(ActionEvent e)
{

 if(name.equals("Quit"))
 {
  //Show an information panel with the options yes/no and message 'choose one':
  int choice = JOptionPane.showConfirmDialog(null,"Are you sure of that?", "RE-CONFIRM", JOptionPane.YES_NO_OPTION);
 
 if (choice == JOptionPane.YES_OPTION )
{
  System.exit();
}
else
{
//something else
}
}

}


(see http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html)
0
 

Author Comment

by:heuer
ID: 6255411
Perfect. Thanks a lot mattyk.
0
 

Author Comment

by:heuer
ID: 6256295
What about this new set of codes?

newboy.setName(JOptionPane.showInputDialog("Please enter name of boy:") );

The above codes will produce a dialog box that prompts user for a name. 2 buttons will be provided for the user i.e. 'OK' and 'CANCEL'. My program will even display the boy when the user clicks on Cancel. This shouldn't be the case. The boy should only be displayed after the user adds his name and clicks on 'OK'.

How can I do a check here like the previous question. If the user clicks on Cancel then the boy will not be added to the screen.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question