Search code examples
javaarraysswingjcombobox

JCombobox accepting array of characters


I want JCombobox to accept an array of characters. I can't seem to find a workaround for this. Basically, I want JCombobox to hold values '0' - '9', but as chars instead of int.

char[] levels = {'0', '1', '2', '3'};
skillLevelCombo = new JComboBox<Object>(levels); //Does not work

How do I get around this? Do I make a Character array? If so, how do I get the char values later on?


Solution

  • This works just fine:

      Character[] levels = {'0', '1', '2', '3'};
      skillLevelCombo = new JComboBox<Character>(levels);
    

    e.g.,

    import javax.swing.JComboBox;
    import javax.swing.JOptionPane;
    
    public class ComboFun {
       private static JComboBox<Character> skillLevelCombo;
    
       public static void main(String[] args) {
          Character[] levels = {'0', '1', '2', '3'};
          skillLevelCombo = new JComboBox<Character>(levels); 
    
          JOptionPane.showMessageDialog(null, skillLevelCombo);
       }
    }
    

    Please note that Integers work well too:

      Integer[] levels = {0, 1, 2, 3};
      final JComboBox<Integer> skillLevelCombo = new JComboBox<Integer>(levels);