Pasindu
Pasindu

Reputation: 340

how to filter content in 2nd combo box according to 1st combo box selection

I want to filter the degrees when the user select Undergraduate or Postgraduate. I searched through internet, but I couldn't find a clear answer with a sample code.

      private String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
    private String[] itemsPostgraduate = new String[]{"BA", "Msc"};
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
   UPselect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
    String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
    String[] itemsPostgraduate = new String[]{"BA", "Msc"};
    String s = (String) UPselect.getSelectedItem();
    if (s.equals("Undergraduate Degrees")){
        //Assign the first list to the combobox
        jComboBox1 = new JComboBox(itemsUndergraduate);
    }
    else{
        //Assign the second list to the combobox
        jComboBox1 = new JComboBox(itemsPostgraduate);
    }
}

});

This is my code so far, how can I fix this?

Upvotes: 1

Views: 384

Answers (2)

user2575725
user2575725

Reputation:

It is good practice to have model and modifying data at model rather than updating UI directly, following is an example for the same using DefaultComboBoxModel.

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.SwingUtilities;

public class Demo{
    public static void main(String[]args){
        SwingUtilities.invokeLater(()->{
            JFrame frame = new JFrame("Dropdown Demo");
            frame.getContentPane().setLayout(new FlowLayout());
            final String SCIENCE = "Science";
            final String COMMERCE = "Commerce";
            final String SELECT = "Choose";
            frame.getContentPane().add(new JLabel("Stream"));
            JComboBox<String> streams = new JComboBox<>(new String[]{SELECT,SCIENCE,COMMERCE});
            frame.getContentPane().add(streams);
            frame.getContentPane().add(new JLabel("Subjects"));
            DefaultComboBoxModel<String> subjectsModel = new DefaultComboBoxModel<>(new String[]{SELECT});
            JComboBox<String> subjects = new JComboBox<>(subjectsModel);
            frame.getContentPane().add(subjects);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            final String[] SCIENCE_SUBJECTS = {"Maths","Biology","Physics","Chemistry"};
            final String[] COMMERCE_SUBJECTS = {"Economics","Accounts","Taxation"};
            streams.addActionListener((e)->{
                SwingUtilities.invokeLater(()->{
                    subjectsModel.removeAllElements();
                    subjectsModel.addElement(SELECT);
                    String[] temp = {};
                    if(SCIENCE.equals(streams.getSelectedItem())){
                        temp = SCIENCE_SUBJECTS;
                    } else if(COMMERCE.equals(streams.getSelectedItem())){
                        temp = COMMERCE_SUBJECTS;
                    }
                    for(String sub : temp){
                        subjectsModel.addElement(sub);
                    }
                    frame.pack();
                });
            });
        });
    }
}

On launch:without selection

On Change:enter image description here

Upvotes: 1

sorifiend
sorifiend

Reputation: 6307

In response to your comments and updated code, yes you are on the right path.

Here is an example. First, we need have two lists that we can use later.

String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
String[] itemsPostgraduate = new String[]{"BA", "Msc"};

Now when the first combo box is selected we can change the contents of the second combo box to match one of the lists:

UPselect.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        String s = (String) UPselect.getSelectedItem();

        //Added this line to help you debug the code
        System.out.print("Does this bit of code ever happen??");
        System.out.print("Value of selected item is: "+s);

        if (s.equals("Undergraduate Degrees")){
            //Assign the first list to the combobox
            jComboBox1 = new JComboBox(itemsUndergraduate);
        }
        else{
            //Assign the second list to the combobox
            jComboBox1 = new JComboBox(itemsPostgraduate);
        }
    }
}

Upvotes: 1

Related Questions