Populate a JComboBox with an int[]

前端 未结 2 1140
傲寒
傲寒 2021-01-27 16:05

Is it possible to populate a JComboBox with an int[]? I am writing a code that will use a JComboBox populated with years (in integers).

The code I have written is this:

2条回答
  •  臣服心动
    2021-01-27 16:55

    one alternative if you just want to work with integers:

    JComboBox comboBox = new JComboBox<>();
    
    comboBox.addItem(1);
    comboBox.addItem(2);
    

    otherwise, you can also try this:

    String[] birthYear = new String[currentYear]; //currentYear is an int variable
    int inc=1;
    for(int i=0;i

    and you can use Integer.praseInt("1") to get string representations of a number as a type int if you need it.

    hope this helps.

提交回复
热议问题