java - How can I use the response from a JOptionPane?


Question: 

This is the first time I have used a confirm box and I'd like some advice on how to use it please, I want to use the users input of "Yes or No" but not sure how to do it? If I wanted to reference the input from the JOptionPane in an if statement how would go about it?

JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);

Thank you.




2 Answers: 

use it like:

   int result = JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);

    if (JOptionPane.YES_OPTION == result) {
                System.out.println("yes");
     } else if (JOptionPane.NO_OPTION == result) {
                System.out.println("No");
     }else{
            System.out.println("Nothing");
    }

Also find the option types and return values below(from source):

 /**
     * Type meaning Look and Feel should not supply any options -- only
     * use the options from the <code>JOptionPane</code>.
     */
    public static final int         DEFAULT_OPTION = -1;
    /** Type used for <code>showConfirmDialog</code>. */
    public static final int         YES_NO_OPTION = 0;
    /** Type used for <code>showConfirmDialog</code>. */
    public static final int         YES_NO_CANCEL_OPTION = 1;
    /** Type used for <code>showConfirmDialog</code>. */
    public static final int         OK_CANCEL_OPTION = 2;

    //
    // Return values.
    //
    /** Return value from class method if YES is chosen. */
    public static final int         YES_OPTION = 0;
    /** Return value from class method if NO is chosen. */
    public static final int         NO_OPTION = 1;
    /** Return value from class method if CANCEL is chosen. */
    public static final int         CANCEL_OPTION = 2;
    /** Return value form class method if OK is chosen. */
    public static final int         OK_OPTION = 0;
    /** Return value from class method if user closes window without selecting
     * anything, more than likely this should be treated as either a
     * <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
    public static final int         CLOSED_OPTION = -1;

Also, don't do int check directly for response value, like if(1==result) for NO_OPTION, always use constants from the JoptionPane class.

 

If you want to learn more about Swing Dialog then please have a look at the below section of Swing Tutorial where it is explained in detail along with a lot examples.

 

More Articles


c# - Set time limit for items in container in CosmosDB using Microsoft.Bot.Builder.Azure (bot framework v4)

I want to set the time to live for the items in a container in cosmosDB, but I am not sure how to do so using the config of cosmosdb storage in bot framework v4. I would appreciate some help on this matter.I have read about setting time to live using DocumentClient() as documented here. However I se

java - Paypal expressCheckout integration (DoExpressCheckout) error

I have a ExpressCheckout integration setup in java google app engine and it working up to GetExpresschekout .but in finall call(DoExpressCheckout) I am unable to Authorize payment .it returnsan errorRESULT=1000&PNREF=E24P1FBB8FA4&RESPMSG=Generic processor error: 13116-The transaction is in p

actions on google - Implementing Conversation Webhook as Azure Function App

I have an azure function app that I am using as a webhook for my google assistant action. I've tried to follow the documentation for the proper response but I continually get the following error in the simulator when testing my webhook. Is there anything in my response message that looks wrong?Fai


node.js - Correct way to send response when using Actions-on-google with koa.js

I'm trying to use actions-on-google with koa framework. I can't understand which request and response to use with AoG constructor in koa middleware. This is my code:const Koa = require( 'koa' )const koaBody = require('koa-body')const { DialogflowApp } = require('actions-on-google')const koaApp = new

Logs showing WARNING when trying to credit bank accounts with Balanced Payments

Been seeing this in my logs today. ############################################################# # WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! # ############################################################# Using positional arguments is **DEPRECATED**. Please use the keyword optio

dialogflow - how to define 'App' statement in firestore database?

I have to define 'app' inorder to store user details from dialogflow to firestore databaseconst app = new App({request:request, response:response});inorder to use this line of code i need 'App' to be defined in firestore code,for which i have used this line,const App = require('firebase-functions')


Magento: Where does Magento 1.8 store payment method options in the database?

Where does Magento store payment method options in the database?I looked around and did not find it.

java - JFrame as a dialog

I want to customize the jframe and that frame should work like a joptionpane. which is relative with parent. can i do like this. if it is possible please any one help me.

Magento Payment methods are not showing in checkout

I have a website in magento. iam using payment methods paypal, bank transfer and stripe. using multi store view(3)Except paypal remaning payment methods are not showing in checkout process. i dont know what to do. please help me any one,Thanks,

payment processing - pylons/python

I'm building an application that eventually needs to process cc #s. I'd like to handle it completely in my app, and then hand off the information securely to my payment gateway. Ideally the user would have no interaction with the payment gateway directly.Any thoughts? Is there an easier way?