我正在努力将数据插入到一个带有 int[] 的组合框中。

huangapple go评论84阅读模式
英文:

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&lt;Integer&gt; cmbTenant = new ArrayList&lt;&gt;();
        ResultSet rs = db.getResults(&quot;SELECT TenantId FROM tblTenant&quot;);
        try {
            while (rs.next()){
                cmbTenant.add(rs.getInt(&quot;TenantID&quot;));
                
                }
                
            
        } catch (SQLException ex) {
            Logger.getLogger(Populate.class.getName()).log(Level.SEVERE, null, ex);
        }
        int[] returnID = new int [cmbTenant.size()];
                for (int i = 0; i &lt; 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.

huangapple
  • 本文由 发表于 2020年4月6日 21:36:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定