我正在尝试将数据库表(称为 stock_info)中的列(称为部分)的值读取到位于JComboBox
(JTable
称为tblContents
)中的 a 中。我的代码如下。运行文件时,组合框出现在表中,当单击它们选择一个选项时,它们会扩展为 8 个空白选项,因为数据库表中的列中有 8 个值。
谁能解释为什么选项都是空白的?
import java.sql.*;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
public class Sale extends javax.swing.JFrame {
PreparedStatement pst = null;
Connection conn = null;
ResultSet rs = null;
int count = 0;
public void FillCmbs() {
String sql = "SELECT * from stockinfo ORDER BY parts";
int size = 0;
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
size++;
}
String[] items = new String[size];
while (rs.next()) {
items[count] = rs.getString("parts");
count++;
}
JComboBox jcb = new JComboBox(items);
TableColumn tc = tblContents.getColumnModel().getColumn(1);
TableCellEditor tce = new DefaultCellEditor(jcb);
tc.setCellEditor(tce);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
}
public Sale() {
initComponents();
conn = DBConnect.ConnectDB();
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
FillCmbs();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sale().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblContents;
// End of variables declaration
}