带有Checkbox的Java结果集到JTable [英] Java Resultset to JTable with Checkbox

查看:108
本文介绍了带有Checkbox的Java结果集到JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码可以显示数据库中的数据。它工作得很好,但我希望它在最后一栏有复选框。我在这里找到了一些代码,但它只适用于预先定义的非值而不是数据库。 (



代码:

 公开打印(){
initComponents();
try {
conn =(Connection)db_connect.connectDB();
}
catch(ClassNotFoundException | SQLException ex){
JOptionPane.showMessageDialog(null,ex);
}
update_table(选择姓名,部分,student_number,来自学生的性别);
}

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);
}
}


解决方案

最简单的方法是不使用DBUtils并将 ResultSet 中的数据自己加载到TableModel中。



代码并不困难,您可以使用数据库表示例 -from-database /rel =nofollow noreferrer>数据库表作为起点。



代码只是将ResultSet中的数据加载到向量,然后您可以手动添加另一列以包含布尔数据。



对代码的更改将类似于:

  //获取列名

for(int i = 1; i< = columns; i ++)
{
columnNames.addElement(md.getColumnName(i));
}

columnName.addElement(Check Mark); //添加

//获取行数据

while(rs.next())
{
Vector< Object> row = new Vector< Object>(columns);

for(int i = 1; i< = columns; i ++)
{
row.addElement(rs.getObject(i));
}

row.addElement(Boolean.FALSE); //添加了
data.addElement(row);
}

另一种选择是创建一个包装布尔列的包装器TableModel使用您的DBUtils TableModel。查看如何在填充的Jtable中添加复选框rs2xml 这个方法的一个例子。



该答案将复选框列放在表的开头,因此您需要将代码修改为将复选框放在最后。


I have this code to which it can display data from database. It's working well but I want it to have checkbox at last column. I've found some codes here but It's only for pre-defined not values and not from database. (How to add checkboxes to JTABLE swing)

Screenshot:

Code:

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);    
    }
}

解决方案

The easiest way is to NOT use DBUtils and to load the data from the ResultSet into the TableModel` yourself.

The code is not difficult and you can use the Table From Database Example found in Table From Database as the starting point.

The code just loads the data from the ResultSet into Vectors, so you can then manually add another column to contain Boolean data.

The changes to the code would be something like:

//  Get column names

for (int i = 1; i <= columns; i++)
{
    columnNames.addElement( md.getColumnName(i) );
}

columnName.addElement( "Check Mark" ); // added

//  Get row data

while (rs.next())
{
    Vector<Object> row = new Vector<Object>(columns);

    for (int i = 1; i <= columns; i++)
    {
        row.addElement( rs.getObject(i) );
    }

    row.addElement( Boolean.FALSE ); // added
    data.addElement( row );
}

The other option is to create a "wrapper" TableModel that wraps a Boolean column with your DBUtils TableModel. Check out How to add checkbox in Jtable populated using rs2xml for an example of this approach.

That answer places the check box column at the start of the table, so you would need to modify the code to place the check box at the end.

这篇关于带有Checkbox的Java结果集到JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆