如何通过SQL将JCheckBox的列添加到JTable? [英] How do I add a column of JCheckBox to a JTable via SQL?

查看:106
本文介绍了如何通过SQL将JCheckBox的列添加到JTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从存储在SQL数据库中的数据创建了JTable.基本上,我的列数固定在JTable中.现在,我想添加一列,该列将允许用户使用特定行的复选框来选择特定行. 我在网上搜索,但没有针对此问题的任何解决方案. 我在SO中进行搜索,而我所得到的就是如何为使用2D数组而不是SQL数据库初始化的JTable添加列.

I have created a JTable from data I stored in a SQL database. Basically, my number of columns are fixed in the JTable. Now I want to add a column which will allow the user to select a particular row using checkbox of that particular row. I searched through the net I did not get any solution for this problem. I searched in SO and all I got is how to add a column for JTable initialized using 2D array but not a SQL database.

我已附加了用于创建JTable的代码.我认为了解我的问题就足够了.

I have attached the code which I am using to create my JTable. I think it is sufficient to understand my problem.

我尝试手动添加一列.它确实添加了一个列.但是现在我的问题是我分配给每一行的复选框显示为javax.swing.JCheckBox,而不是该列中的复选框图标".

I have tried adding a column manually. It did add a column. But now my issue is that the checkboxes I assign to each row appear as javax.swing.JCheckBox instead of the "checkbox icon" in the column.

public void init_table(JTable X)
{
try
{
    Class.forName(JDBC_DRIVER);
    con= DriverManager.getConnection("jdbc:mysql://localhost:3306/store",DB_USER, DB_PASS);
    query="SELECT * from Stalk";
    stmt = con.createStatement();
    rs = stmt.executeQuery(query);
    DefaultTableModel model= new DefaultTableModel();
    ResultSetMetaData meta = rs.getMetaData();
    int Columncount = meta.getColumnCount();
    for(int columnindex=1; columnindex<=Columncount; columnindex++)
    {
        model.addColumn(meta.getColumnLabel(columnindex));
    }
    Object[] row= new Object[Columncount];
    while(rs.next())
    {
    int i=0;
    for(i=0;i<Columncount;i++)
    {
        row[i]=rs.getObject(i+1);
    }

    model.addRow(row);
        }
    X.setModel(model);
        } 
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

推荐答案

从此完整的示例开始,以下更改产生说明的结果.注意:

Starting from this complete example, the changes below produce the result illustrated. Note:

  • 针对SELECTED列的getColumnClass()的实现返回此处.

  • The implementation of getColumnClass() for the SELECTED column returns a type token of Boolean.class, for which the default renderer uses a check box; more here.

ResultSet包括类型为Boolean的新列.

city具有类型为boolean的新列,并已初始化为随机状态.

The table city has a new column of type boolean, initialized to a random state.

$ diff OldTest.java WorkerTest.java 
48a49
>         Boolean selected;
91a93,94
>                 case 2:
>                     return row.selected;
105a109,121
>         @Override
>         public Class<?> getColumnClass(int colIndex) {
>             switch (colIndex) {
>                 case 0:
>                     return Object.class;
>                 case 1:
>                     return String.class;
>                 case 2:
>                     return Boolean.class;
>             }
>             return null;
>         }
> 
114a131
>                         r.selected = rs.getBoolean(3);
138c155
<             st.execute("create table city(id integer, name varchar2)");
---
>             st.execute("create table city(id integer, name varchar2, selected boolean)");
140c157
<                 "insert into city values (?, ?)");
---
>                 "insert into city values (?, ?, ?)");
144a162
>                     ps.setBoolean(3, r.nextBoolean());

这篇关于如何通过SQL将JCheckBox的列添加到JTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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