用int填充JComboBox [] [英] Populate a JComboBox with an int[]
问题描述
是否可以用int []填充JComboBox?我正在编写一个代码,该代码将使用以年份(整数)填充的JComboBox.
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).
我写的代码是这样的:
int[] birthYear = new int[currentYear]; //currentYear is an int variable
int inc=1;
for(int i=0;i<currentYear;i++)
{
birthYear[i]= inc;
inc++;
}
birthYearBox = new JComboBox(birthYear); //this is the line in which the error occurs
我希望它们在ComboBox中为整数,以便可以从中减去.我是否必须用字符串填充ComboBox,然后在输入字符串后使它们成为整数?还是有办法用int []实际填充ComboBox?
I want these to integers in the ComboBox so that I can subtract from them. Do I have to populate the ComboBox with strings, then make them integers after they have been input? Or is there a way to actually populate the ComboBox with the int[]?
推荐答案
JCombobox
是泛型的,但是Java泛型不支持原始类型(而int
是原始类型).
JCombobox
is generic, but Java generics don't support primitive type (and int
is a primitive type).
因此,请使用Integer
数组:
Integer[] birthYear = new Integer[currentYear]; //currentYear is an int variable
int inc=1;
for(int i=0;i<currentYear;i++){
birthYear[i]= inc;
inc++;
}
JComboBox<Integer> birthYearBox = new JComboBox<>(birthYear);
这篇关于用int填充JComboBox []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!