option pane questions

Posted on 2004-10-12
Last Modified: 2010-03-31
hi experts

I have two questions:

can you get an option pane with a check box in it that will affect the outcome? for example if it is checked then you save a certain part of the program and if not it saves the default..whatever..

second I need an example of an option pane that would have three buttons. yes, no, and cancel and how to know know which button was pressed and accordingly take action

Question by:Samooramad
  • 4
LVL 37

Accepted Solution

zzynx earned 400 total points
ID: 12286830
For the 2nd:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

 * @author  zzynx
public class JOptionPaneDemo2 extends JFrame {
    public JOptionPaneDemo2() {
        setSize(500, 400);

    private void init() {
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
        JButton button = new JButton("Go");
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
        getContentPane().setLayout(new FlowLayout());
    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane p = new JOptionPane("Are you sure you want to log out?",
            JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION);
        final JDialog d = p.createDialog(this, "Report");
        Object value = p.getValue();
        if (value == null || !(value instanceof Integer)) {
            JOptionPane.showMessageDialog(null, "You closed without answering");
        else {
            int i = ((Integer)value).intValue();
            if (i == JOptionPane.DEFAULT_OPTION) {
                JOptionPane.showMessageDialog(null, "You pressed ESC");
            else if (i == JOptionPane.NO_OPTION) {
                JOptionPane.showMessageDialog(null, "You choose \"No\"");
            } else if (i == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(null, "You choose \"Yes\"");
            } else if (i == JOptionPane.CANCEL_OPTION) {
                JOptionPane.showMessageDialog(null, "You choose \"Cancel\"");

    public static void main(String args[]) {
        JOptionPaneDemo2 f = new JOptionPaneDemo2();;
LVL 37

Expert Comment

ID: 12286864
For the 1st:

    import javax.swing.*;
    import java.awt.*;

    public class DialogBox
        public static void main(String[] args)
            JPanel fields = new JPanel();
            JLabel label = new JLabel("Some text:");
            JCheckBox cb = new JCheckBox();

            String[] options = {"Yes", "No", "My Cancel"};
            int input = JOptionPane.showOptionDialog(
                null,   // parentComponent
                fields, // message
                "Enter your input", // title
                JOptionPane.YES_NO_CANCEL_OPTION,   // optionType
                JOptionPane.QUESTION_MESSAGE,   // messageType
                null,   // icon
                options,    // options
                options[0]  // initialValue

            if (input == 0) {
                System.out.println("Yes pressed. CheckBox selected = " + cb.isSelected());
            else if (input == 1) {
                System.out.println("No pressed. CheckBox selected = " + cb.isSelected());
            else if (input == 2) {
                System.out.println("Cancel pressed. CheckBox selected = " + cb.isSelected());

LVL 37

Expert Comment

ID: 12286876
Next time you better ask one question per ... errrh question ;°)

Author Comment

ID: 12290049
yeah I should I guess :)

thanks for the help
LVL 37

Expert Comment

ID: 12290725

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 30
egit plugin on eclipse 8 82
Java syntax, or is it Selenium 6 30
learn programming 8 42
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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