?
Solved

Does a preset Yes/No dialog similar to JOptionPane.showInputDialog exist in the Java API?

Posted on 2009-02-12
4
Medium Priority
?
408 Views
Last Modified: 2013-11-23
I was wondering if a preset Yes/No dialog exist in the Java API? Something similar to JOptionPane.showInputDialog where the dialog is automatically created, centered and showed and the input is obtained a la input = JOptionPane.showInputDialog. I have made one myself, but the process seems cumbersome compared to what it is. Furthermore in order to get the input I have to save it in a field an do a setvisible(false) and THEN dispose it from its parent..there must be a better way? I have attached my own YesNoDialog.
public class YesNoDialog extends JDialog implements ActionListener {
 
	private static final long serialVersionUID = -1585924235633312637L;
	
	private boolean answer;
	
	public DoekIncYesNoDialog(Frame owner, String title, String text, boolean modal) {
		super(owner, title, modal);		
		JPanel messagePane = new JPanel();
		messagePane.add(new JLabel(text));
		getContentPane().add(messagePane);		
		JPanel buttonPane = new JPanel();
		JButton btnYes = new JButton("Yes");
		btnYes.addActionListener(this);
		buttonPane.add(btnYes);		
		JButton btnNo = new JButton("No");
		btnNo.addActionListener(this);
		buttonPane.add(btnNo);				
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		pack();
	}
	
	public boolean getAnswer(){
		return answer;
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		if (((JButton)e.getSource()).getText()=="Yes") {
			this.answer = true;
		} else {
			this.answer = false;
		}
		setVisible(false);
	}
 
}

Open in new window

0
Comment
Question by:Suppai
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 1200 total points
ID: 23624479
Hi Suppai,

To get a pure Yes/No dialog, use this method:

http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)

and pass JOptionPane.YES_NO_OPTION as the optionType parameter.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 800 total points
ID: 23624583
e.g. as CP says:
JOptionPane.showConfirmDialog(this, "Wanna do it?", "", JOptionPane.YES_NO_    OPTION);

Open in new window

0
 

Author Closing Comment

by:Suppai
ID: 31546189
Exactly what I needed!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23625961
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 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