我有这个代码,它可以显示数据库中的数据。它运行良好,但我希望它在最后一栏有复选框。我在这里找到了一些代码,但它仅用于预先定义的非值,而不是来自数据库。 (How to add checkboxes to JTABLE swing)Java结果集与JTable与复选框
截图:
代码:
public print() {
initComponents();
try{
conn = (Connection) db_connect.connectDB();
}
catch(ClassNotFoundException | SQLException ex){
JOptionPane.showMessageDialog(null, ex);
}
update_table("select name, section, student_number, gender from students");
}
public void update_table(String q){
try{
st= conn.createStatement();
st.executeQuery(q);
ResultSet rs = st.executeQuery(q);
users_list.setModel(DbUtils.resultSetToTableModel(rs));
users_list.getColumnModel().getColumn(0).setPreferredWidth(250);
users_list.getColumnModel().getColumn(0).setPreferredWidth(250);
users_list.getColumnModel().getColumn(1).setPreferredWidth(150);
users_list.getColumnModel().getColumn(2).setPreferredWidth(120);
users_list.getColumnModel().getColumn(3).setPreferredWidth(100);
int count= users_list.getModel().getRowCount();
if(count==0){
no_results_found.setVisible(true);
}
else{
no_results_found.setVisible(false);
}
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,ex);
}
}
国际海事组织你首先需要在最后一栏有一个复选框的表。然后你想根据数据库值设置复选框。所以请用表格显示你的代码,包括复选框。 – 0X0nosugar
这是不相关的,但是你执行查询两次 –
可能重复的[如何呈现JTable中的复选框?](http://stackoverflow.com/questions/4154802/how-to-render-a-checkbox -in-A-的JTable) – roeygol