Link to home
Start Free TrialLog in
Avatar of Samooramad
Samooramad

asked on

option pane questions

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
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
        }
    }
Next time you better ask one question per ... errrh question ;°)
Avatar of Samooramad
Samooramad

ASKER

yeah I should I guess :)

thanks for the help
Thanks