我使用以下代码仅获取 mysql 表的列表并将它们添加到 JComboBox。但我得到的只是 JComboBox 中的“[]”!请帮助我
ArrayList<String> tables = new ArrayList<String>();
public ArrayList<String> tablesArray() throws SQLException
{
ResultSet result = connecting.getMetaData().getCatalogs();
while(result.next())
{
tables.add(result.getString(0));
}
return tables;
}
public JComboBox comboBox()
{
JComboBox<ArrayList> combo = new JComboBox<ArrayList>();
combo.addItem(tables);
combo.setBounds(130,30,190,30);
return combo;
}
最佳答案
ArrayList tables = new ArrayList();
是包含一个或多个项目的数组
combo.addItem(tables);
后来你添加了 a.m. 数组作为 Item,那么输出可能是正确的
将数组作为构造函数传递,更多信息请参见 JComboBox API Constructor Summary
最好创建一个 DefaultComboBoxModel(请参阅上午 API)并将这些项目从 ResultSet 直接添加到模型
关于java - 如何在JComboBox中获取mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693062/