We help IT Professionals succeed at work.

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

Suppai
Suppai asked
on
Medium Priority
424 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

Comment
Watch Question

Senior Java Architect
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016
Commented:
e.g. as CP says:
JOptionPane.showConfirmDialog(this, "Wanna do it?", "", JOptionPane.YES_NO_    OPTION);

Open in new window

Author

Commented:
Exactly what I needed!
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.