Solved

return value of a dialog box.

Posted on 2001-07-04
3
235 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 50 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 24
Android development question 2 81
ejb entity bean example 2 38
printf performancy 11 86
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

730 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