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