[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

return value of a dialog box.

// 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
heuer
Asked:
heuer
  • 2
1 Solution
 
mattykCommented:
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
 
heuerAuthor Commented:
Perfect. Thanks a lot mattyk.
0
 
heuerAuthor Commented:
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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now