[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

return value of a dialog box.

Posted on 2001-07-04
3
Medium Priority
?
251 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

Technology Partners: 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

649 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