-1

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.

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

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

}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Well My question states the very problem I encounter. Every solution shows me to set my table model using 2D array. But as my code states I am not using 2D array to initialize the columns but sql database. I want to know a method which will allow me to add a column of jcheckboxes. I dont want to know how to set a column of JCheckboxes while making/creating the JTable. Please Help! – Sagar Jain Apr 22 '19 at 07:00
  • 1
    The method you apply the checkboxes to your columns is the same regardless of you model, hence the reason you will continue to be directed to previous questions. The problem has nothing to do with your model and everything to do with your misunderstand of the api and SO is not the best place to correct that. Start with tutorials, look at the other examples and adapt there solutions to you problem – MadProgrammer Apr 22 '19 at 07:04
  • @MadProgrammer is correct; I've illustrated the essential requirement below in complete example. – trashgod Apr 22 '19 at 10:48

1 Answers1

1

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

  • 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.

  • The ResultSet includes a new column of type Boolean.

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

image

$ 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());
trashgod
  • 203,806
  • 29
  • 246
  • 1,045