Search code examples
javaswingjcomboboxcomboboxlist

Can I create List of comboBox type?


Good day everybody

Can I create List of JCombobox type .. and fill it with comboBoxes like this or something ?

List<JCombobox> comboBoxList = new arrayList<JComboBox>();
comboBoxList.add(JComboBox); 

or something like above???


Solution

  • Yes you can have an ArrayList which can contain JComboBox type elements

    You can create ArrayList like this:

      List<JComboBox> comboBoxList = new ArrayList<>(); // you can also provide capacity here
    

    And then you can create JComboBox instances by using any of the constructors

      JComboBox combo1 = new JComboBox();
      // set properties of JComboBox here
    

    And then you can add this to your comboBoxList by

      comboBoxList.add(combo1);
    

    Hope this helps.