8

Have a method in my GUI project for displaying a JOptionPane with several components on it. Two of these components are ButtonGroups with two JRadioButtons in each, in the first group the first button is selected by default, and in the second group the second button is selected by default. In the second group I want to have the first button disabled until the second button in the first group is selected. i.e. if the user is happy with the default selection in BG1, then they can't make a selection in BG2, only if they make the second selection in BG1 can they have the other option in BG2.

Is this type of behavior possible with a JOptionPane?

Have been looking at the tutorials for JDialog, JOptionPaneand doing other research but none of these have proved helpful in my case.

Ben the Coder
  • 539
  • 2
  • 5
  • 21
  • 2
    *"Is this type of behavior possible with a `JOptionPane`? "* Sure. do it much the same way you would do it in a `JFrame`. If you cannot achieve it in a frame, post an [SSCCE](http://sscce.org/) of your best attempt. – Andrew Thompson Jan 19 '13 at 07:20
  • 5
    The message parameter of `JOptionPane` takes a `Object`. If you pass it a `Component`, the `JOptionPane` will use it as the "main" view, adding the icon and buttons around it – MadProgrammer Jan 19 '13 at 08:41
  • 12
    If you are going to answer your own question, please post your answer as an answer and accept your answer. (That way people don't waste time reading your question in hopes of answering it) – Gus Mar 10 '13 at 16:57

3 Answers3

0

i do not think it is posible with a JOption. but i think it is posible with a JDialog.

examble:

when you open the dialog you could use the command JFrame(here you have to write your window name).enable(false)

you could get it to have close-button in the couner you could have a check box when the check box is true. it will show a button and when you click it could make the button invisble

  • This is definitely a wrong assumption. But as JOptionPane only takes one Object as the message, ensure it is a JComponent (speak: JPanel) that contains all the information and behaviour the OP desires). It will render and behave well. – Queeg Jul 28 '23 at 06:58
0

Best Thing is go for JDialog

Because JOptionPane is not supporting for this much of components.

Chinthaka Dinadasa
  • 3,332
  • 2
  • 28
  • 33
  • The reason for going to JDialog is if JOptionPane is not flexible enough (e.g. you need a resizable window). Use a JPanel to wrap multiple components into one and pass it into JOptionPane. – Queeg Jul 28 '23 at 07:01
0

In the elective.addActionListener statement I called the wrong variable, had cp12 instead of cp6, posted my cut down code below, all is good, thanks

    public void displayAddSomething(ArrayList<String> items)
    {
// cut down version only showing the button panels

    // initialising the variables       
    JPanel buttPanel = new JPanel(new GridLayout(0, 1));
    JPanel pointPanel = new JPanel(new GridLayout(0, 1));
    JRadioButton core = new JRadioButton("Core Item: ", true);
    final JRadioButton elective = new JRadioButton("Elective Item: ");
    final JRadioButton cp6 = new JRadioButton("6 Points: ");
    JRadioButton cp12 = new JRadioButton("12 Points: ", true);
    ButtonGroup bg1 = new ButtonGroup();
    ButtonGroup bg2 = new ButtonGroup();

    // adding the buttons to buttPanel  and the button group 1
    buttPanel.add(new JLabel("Select Course Type"));
    buttPanel.add(core);
    buttPanel.add(elective);
    buttPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
    bg1.add(core);
    bg1.add(elective);

    // add buttons pointPanel and bg2
    pointPanel.add(new JLabel("Select Credit Points"));
    pointPanel.add(cp6);
    pointPanel.add(cp12);
    pointPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
    bg2.add(cp6);
    bg2.add(cp12);

    cp6.setEnabled(false);


    // add action listener for each of bg1 buttons so if event
    // occurs the cp6 button will toggle between enabled and disabled.

    elective.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {   
             cp6.setEnabled(true);
        }});

    core.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            cp6.setEnabled(false);
        }});

    Object[] message = {buttPanel,pointPanel};

    int result = JOptionPane
        .showOptionDialog(
        this,
        message,
        "...some text...",
        JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE,
        null, new String[] { "Submit", "Cancel" }, "Default");

    if (result == JOptionPane.OK_OPTION)
        {
        // collecting all the input values in a string array to pass to
            // another class for processing by the model... 
        }
    else if (result == JOptionPane.NO_OPTION)
        {
             JOptionPane.showMessageDialog(this,
             "You Have Elected Not to do anything At This Time",
             "YOU HAVE COME THIS FAR AND YOU QUIT NOW....",
            JOptionPane.QUESTION_MESSAGE);                     
        }
    }
Matt
  • 74,352
  • 26
  • 153
  • 180