1

How can I use a RadioButton to select what appears in the combobox that is displayed after i.e. click (buttons(male) or (female)) and get a combo box drop down displaying either (combobox(Mr, master) or (Mrs,miss))

Ive spent the past few hours trying to figure out something at would work. I thought all I needed was to create an actionListener to set a different array to the combobox depending on what button is clicked but I seemed to get nowhere.

This is what I had so far

   `male = new JRadioButton("Male", true); 
    female = new JRadioButton("Female", false);
    add(male);
    add(female);

    buttonGroup = new ButtonGroup();
    buttonGroup.add(male);
    buttonGroup.add(female);

    radioListener radio = new radioListener();

    male.addActionListener(radio);
    female.addActionListener(radio);

    JComboBox nameTitle = new JComboBox(); 

    add(nameTitle);`

}

public class radioListener implements ActionListener{

    public void actionPerformed(ActionEvent e) {
        if(male.isSelected()) nameTitle.setSelectedItem(maleTitles);

        if(female.isSelected()) nameTitle.setSelectedItem(femaleTitles);
    }

}
jammypotato
  • 65
  • 1
  • 7
  • 1
    `I thought all I needed was to create an actionListener ` - that is correct. `but I seemed to get nowhere` - well then post your code showing what your tried. We can't guess what you are doing. That is post a [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Oct 23 '15 at 00:01
  • Sorry it had been a long night the edit shows what I thought was my closest attempt to being on the right track – jammypotato Oct 24 '15 at 23:21
  • The edit doesn't help. Did you read the link??? How does that code compile? – camickr Oct 25 '15 at 00:06

2 Answers2

2

Start with

This will help you setup the buttons, set up button groups and write the ActionListener which will trigger when one of the buttons is selected.

Then move onto How to Use Combo Boxes, which will help you construct appropriate ComboBoxModels which represent the values you want to be associated with in each button.

Now, when the ActionListener is triggered, you choose the appropriate model and apply it to the JComboBox

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • This was not the advice I was expecting but im glad that it was the one I got. After hours of frustration I just wanted to give up and have someone just give me an answer, but instead I was pleasantly surprised to find someone who said no im not going to straight up give you the answer to your problem im going to help you get the info to teach yourself to fix the problem. Now ive finally worked it out for myself it feels far more rewarding then if i had just copyed it not to mention I now have a much better overall understanding of that part of java that I hadn't really used before. So Thanks – jammypotato Oct 25 '15 at 22:53
  • You are very welcome, that attitude will make you an awesome developer! It's very rare to find a answer which is 100% what you want, so learning how to interrupt different, similar solutions, is a very important skill. – MadProgrammer Oct 25 '15 at 22:55
0

This is what worked for me in the end

The approach of using pre-existing arrays and using actionListeners to substitute them into the comboBox didn't seem to work so I came at it from a different angle and used a DefaultComboBoxModel to add and remove elements from the combo box according to the radio button clicked and the actionListeners response

This is not the full code just the main elements as a visual example:

private DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();

    // Male/Female radio buttons
    male = new JRadioButton("Male", true); 
    female = new JRadioButton("Female", false);
    add(male);
    add(female);

    buttonGroup = new ButtonGroup();
    buttonGroup.add(male);
    buttonGroup.add(female);


    // Title box MR/Mrs selection
    radioListener radio = new radioListener();  
    male.addActionListener(radio);
    female.addActionListener(radio);    

// Used to set default combo box if there is no user selection
    model.addElement("Mr");
    model.addElement("Master");

    JComboBox nameTitle = new JComboBox(model);

    add(nameTitle);

    public class radioListener implements ActionListener{

    public void actionPerformed(ActionEvent e) {
        if(male.isSelected()){
            model.removeAllElements();
            model.addElement("Mr");
            model.addElement("Master");
        }

        if(female.isSelected()){
            model.removeAllElements();
            model.addElement("Mrs");
            model.addElement("Miss");
        }
    }
jammypotato
  • 65
  • 1
  • 7