英文:
I am struggling to insert data into a combobox with an int[]
问题
我正在尝试从我的数据库中填充组合框(combo box)的数据,我已经创建了一个方法来进行填充:
public class Populate {
private ConnectDB db = new ConnectDB(); // 实例化db对象
PropertyList prop = new PropertyList();
TenantList ten = new TenantList();
public int[] populatecmbTenant(){
ArrayList<Integer> cmbTenant = new ArrayList<>();
ResultSet rs = db.getResults("SELECT TenantId FROM tblTenant");
try {
while (rs.next()){
cmbTenant.add(rs.getInt("TenantID"));
}
} catch (SQLException ex) {
Logger.getLogger(Populate.class.getName()).log(Level.SEVERE, null, ex);
}
int[] returnID = new int[cmbTenant.size()];
for (int i = 0; i < cmbTenant.size(); i++) {
returnID[i] = cmbTenant.get(i);
}
return returnID;
}
}
但当我尝试在initcomponents()方法中设置值到我的组合框时,出现了错误,错误信息为:“no suitable constructor found for DefaultComboBoxModel(int[])”。我用以下代码来进行填充:
DefaultComboBoxModel Tenant = new DefaultComboBoxModel(pop.populatecmbTenant());
有人可以帮助我修复这个问题吗?
英文:
Hi i am trying to fill my combo bow with data from my database and i have made a methoed to populate it
ublic class Populate {
private ConnectDB db = new ConnectDB(); // instantiates the object db
PropertyList prop = new PropertyList();
TenantList ten = new TenantList();
public int [] populatecmbTenant(){
ArrayList<Integer> cmbTenant = new ArrayList<>();
ResultSet rs = db.getResults("SELECT TenantId FROM tblTenant");
try {
while (rs.next()){
cmbTenant.add(rs.getInt("TenantID"));
}
} catch (SQLException ex) {
Logger.getLogger(Populate.class.getName()).log(Level.SEVERE, null, ex);
}
int[] returnID = new int [cmbTenant.size()];
for (int i = 0; i < cmbTenant.size(); i++) {
returnID[i] = cmbTenant.get(i);
}
return returnID;
}}
but then when i go to set the values into my combo bow int the initcomponents();
it wont let this is the error "no suitable constructor found for DefualtComboBoxModel(int[])
the code i used to populate it was
DefaultComboBoxModel Tenant = new DefaultComboBoxModel(pop.populatecmbTenant());
can anyone help me fix it
答案1
得分: 1
以下是翻译好的部分:
你想要的DefaultComboBoxModel的构造函数如下所述:
构造一个使用对象数组初始化的DefaultComboBoxModel对象。
在Java中,普通的int不是一个对象,所以你需要返回一个Integer数组。
英文:
The constructor you want of the DefaultComboBoxModel is described as the following:
Constructs a DefaultComboBoxModel object initialized with an array of objects.
Normal int is not an object in java so you need Integer[] as the returned array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论