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

Posted on 2009-02-12
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));


		JPanel buttonPane = new JPanel();

		JButton btnYes = new JButton("Yes");



		JButton btnNo = new JButton("No");



		getContentPane().add(buttonPane, BorderLayout.SOUTH);




	public boolean getAnswer(){

		return answer;



	public void actionPerformed(ActionEvent e) {

		if (((JButton)e.getSource()).getText()=="Yes") {

			this.answer = true;

		} else {

			this.answer = false;





Open in new window

Question by:Suppai
    LVL 14

    Accepted Solution

    Hi Suppai,

    To get a pure Yes/No dialog, use this method:,%20java.lang.Object,%20java.lang.String,%20int)

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

    Assisted Solution

    e.g. as CP says:
    JOptionPane.showConfirmDialog(this, "Wanna do it?", "", JOptionPane.YES_NO_    OPTION);

    Open in new window


    Author Closing Comment

    Exactly what I needed!
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    instanceof  operator in java 26 55
    scores100 challenge 3 67
    scoresClump  challenge 31 85
    bigHeights  challenge 13 43
    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…
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now