Solved

option pane questions

Posted on 2004-10-12
5
219 Views
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


thanks
0
Comment
Question by:Samooramad
  • 4
5 Comments
 
LVL 37

Accepted Solution

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

/*
 * JOptionPaneDemo.java
 *
 */

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

/**
 *
 * @author  zzynx
 */
public class JOptionPaneDemo2 extends JFrame {
   
    public JOptionPaneDemo2() {
        init();
        setSize(500, 400);
        setLocationRelativeTo(null);
    }

    private void init() {
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                System.exit(0);
            }
        });
       
        JButton button = new JButton("Go");
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonActionPerformed(evt);
            }
        });
       
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(button);
       
        pack();
    }
   
    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");
        d.setLocationRelativeTo(this);
        d.setVisible(true);
        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();
        f.show();
    }
}
0
 
LVL 37

Expert Comment

by:zzynx
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();
            fields.add(label);
            fields.add(cb);

            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());
            }

            System.exit(0);
        }
    }
0
 
LVL 37

Expert Comment

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

Author Comment

by:Samooramad
ID: 12290049
yeah I should I guess :)

thanks for the help
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12290725
Thanks
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 107
reverse digits of a number using for loop 5 41
Windows 10 IE Certificate Issue 10 45
HashTable highest marks enumeration alternative 9 38
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.

778 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