java combox字体,Java JComboBox不兼容类型:无法转换为字符串

在尝试将数据库中的产品数据加载到JComboBox时,遇到了一个错误:`incompatible types: ComboBox cannot be converted to String`。问题可能在于combobox被声明为接受String类型的JComboBox,但尝试添加的是ComboBox对象。修正方法是声明JComboBox来持有ComboBox类的实例。
摘要由CSDN通过智能技术生成

I have this error when i try to add items in the JComboBox

incompatible types: ComboBox cannot be converted to String

This is my method to load the data from database to the JComboBox...

public final void loadProducts()

{

try

{

String sql = "SELECT * from product";

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

while (rs.next())

{

combobox.addItem(new ComboBox(rs.getString(2), rs.getString(1)));

}

combobox.setSelectedIndex(-1);

}

catch (SQLException ex)

{

System.out.println(ex);

}

}

And this is the class

public class ComboBox

{

private String key;

private String value;

public ComboBox(String key, String value)

{

this.key = key;

this.value = value;

}

@Override

public String toString()

{

return key;

}

public String getKey()

{

return key;

}

public String getValue()

{

return value;

}

}

I have no idea what's causing it! Can someone point out my mistake?

解决方案

It is hard to be certain without knowing how combobox is declared and at which line the Exception is being thrown...

My guess: combobox is declared as a JComboBox that takes a String and you the Exception is being thrown since a ComboBox is being added instead of a String.

Possible correction: declare the JComboBox to hold instances of ComboBox:

private JComboBox combobox;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值