Here is my code:
Object[][] refreshCartonCodesToTable = dbutils.checker.CartonCodesToTable();
String[] colnames = new String[6];
colnames[0] = selectCodes.invoiceTable.getColumnName(0).toString();
colnames[1] = selectCodes.invoiceTable.getColumnName(1).toString();
colnames[2] = selectCodes.invoiceTable.getColumnName(2).toString();
colnames[3] = selectCodes.invoiceTable.getColumnName(3).toString();
colnames[4] = selectCodes.invoiceTable.getColumnName(4).toString();
colnames[5] = selectCodes.invoiceTable.getColumnName(5).toString();
MyTableModel mod = new MyTableModel(refreshCartonCodesToTable, colnames);
selectCodes.invoiceTable = new JTable(mod);
selectCodes.invoiceTable.setVisible(true);
Custom model as shown below:
class MyTableModel extends DefaultTableModel {
public MyTableModel(Object data[][], Object columnames[]) {
super(data, columnames);
}
public Class getColumnClass(int col) {
if (col == 5) {
return Boolean.class;
} else {
return String.class;
}
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == 0) //first column will be uneditable
{
return false;
} else {
return true;
}
}
}
The table displays the columnames
but the data is not diplayed. The array has data and the sample output is as shown below:
250VV 250VV0575W20140819 false B1 19 August 2014
250VV 250VV0561W20140819 false B1 19 August 2014
250VV 250VV0560W20140819 false B1 19 August 2014
250VV 250VV0559W20140819 false B1 19 August 2014
250VV 250VV0558W20140819 false B1 19 August 2014
There are six columns. The sixth column I want to place a checkbox in the cells.
Can somebody help me please.
Here is the source code for CartonCodesToTable();
public static Object[][] CartonCodesToTable() {
Object[][] array = null;
try {
dbutils.checker.connect_to_db_again_again();
sqlcommand = "SELECT Product_ID, carton_code, scanned, batchno,date FROM carton_codes where scanned ='false' order by bno asc";
rset = stmts.executeQuery(sqlcommand);
int row = 0;
while (rset.next()) {
rset.last();
row = rset.getRow();
}
array = new String[row][6];
rset.beforeFirst();
int x = 0;
while (rset.next()) {
array[x][0] = rset.getObject("Product_ID");
array[x][1] = rset.getObject("carton_code");
array[x][2] = rset.getObject("scanned");
array[x][3] = rset.getObject("batchno");
array[x][4] = rset.getObject("date");
array[x][5] = false;
x += 1;
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
return array;
}
When i use array[x][5] = false; i get an error 'java.lang.ArrayStoreException: java.lang.Boolean' So i decided to use array[x][5] = "false";