【发布时间】:2013-10-09 07:19:28
【问题描述】:
我是 Java 初学者。我真的需要从数组或ArrayList 返回DefaultTableModel (javax.swing)。有可能的?我无法将数组插入DefaultTableModel(构造函数)。
代码如下:
private DefaultListModel model;
public DefaultListModel getNamesAndIdToCombobox(Connection conn, boolean closeConn, String sql) throws SQLException {
long counter = 0;
try {
Statement stmt =
conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
// String longKey = (String)rs.getString(2);
try
{
jListList.add(new JListValues(rs.getLong(2), rs.getString(1)));
}
catch(SQLException sqlException){}
try
{
jListList.add(new JListValues(rs.getLong(2), rs.getLong(1)));
}
catch(SQLException sqlException){}
try
{
jListList.add(new JListValues(rs.getString(2), rs.getLong(1)));
}
catch(SQLException sqlException){}
counter++;
}
JListValues[] array = jListList.toArray(new JListValues[jListList.size()]);
model = new DefaultListModel(array); // HERE IT IS A PROBLEM
LOGGER.info("getNamesAndIdToCombobox result count: " + counter);
} catch (SQLException e) {
LOGGER.error("Error", e);
throw e;
} finally {
try {
if (closeConn == true)
conn.close();
} catch (Exception e) {/* null */
}
}
return model;
}
【问题讨论】:
-
到底是什么问题?
-
所以创建一个循环并将值一一复制到Vector,然后使用Vector创建DefaultListModel。或者,只需将值加载到 Vector 中,这样您就不需要进行复制。另外,澄清你的问题。有时您指的是 TableModel,有时您指的是 ListModel。