0

I am having trouble making a checklist inside the JTable, as you can see in the picture it shows false under the "Checked" section i want to make that column a checkbox instead. I have looked up here to see how to do the checklist however i dont know where to start.

enter image description here

public class DosageTableHelper extends AbstractTableModel{

static int counter=1;


public static DefaultTableModel getElderlyFromQueryDos(String search) throws SQLException {
    search = (search.equalsIgnoreCase("")) ? "%" : search;
    SQLObject so = new SQLObject();
    return (DefaultTableModel) buildTableModel(so.getResultSet("SELECT medication FROM et_elderly WHERE name LIKE ?", search));
}
// Method from http://stackoverflow.com/questions/10620448/most-simple-code-to-populate-jtable-from-resultset

@SuppressWarnings("unchecked")
public static AbstractTableModel buildTableModel(ResultSet rs) throws SQLException {
    ArrayList<DosageObject> retrieveDosBlob = null;
    ArrayList<ArrayList> storeList=new ArrayList<ArrayList>();
    SQLObject so = new SQLObject();
    try {
        while(rs.next()){
            //testing column can remove any time
            ResultSetMetaData metaData = rs.getMetaData();

            System.out.println(metaData.getColumnCount());

            PreparedStatement statement = so.getPreparedStatementWithKey("SELECT medication FROM et_elderly WHERE id = ?");
            statement.setInt(1, counter);

            ResultSet rs1 = statement.executeQuery();
            rs1.next();
            ByteArrayInputStream in = new ByteArrayInputStream(rs.getBytes(1));
            ObjectInputStream is;
            is = new ObjectInputStream(in);
            retrieveDosBlob = (ArrayList<DosageObject>) is.readObject();
            storeList.add(retrieveDosBlob);
            counter++;
        }
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // storing array list in an array list for future uses

    Vector<String> columnNames = new Vector<String>();

    columnNames.add("Description");
    columnNames.add("Prescription");
    columnNames.add("Medication Type");
    columnNames.add("Dosage");
    columnNames.add("Checked");

    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    // database print all IDs
    // vector due to table



    ArrayList<DosageObject> nowList=storeList.get(0);

    for(int k=0;k<nowList.size();k++){
        DosageObject dosing=nowList.get(k);
        Vector<Object> vector = new Vector<Object>();
        vector.add(dosing.getMedDescrip());
        vector.add(dosing.getMedPrescrip());
        vector.add(dosing.getMedType());
        vector.add(dosing.getMedDosage());
        vector.add(Boolean.FALSE);
        data.add(vector);

    }




    DefaultTableModel dtm = new DefaultTableModel(data, columnNames) {
        private static final long serialVersionUID = 4234183862785566645L;
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    return dtm;
}

// Debug-able main method
public static void main(String[] args) throws SQLException {
    JTable toDoTable = new JTable(getElderlyFromQueryDos(""));
    JScrollPane jpane = new JScrollPane(toDoTable);
    JPanel panel = new JPanel();
    JFrame frame = new JFrame();
    frame.setBounds(0, 0, 700, 500);
    panel.add(jpane);
    frame.getContentPane().add(new JScrollPane(panel));
    frame.setVisible(true);

}

@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Object getValueAt(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return null;
}
public Class getColumnClass(int column) {
    return (getValueAt(0, column).getClass());
}

}

  • 2
    http://stackoverflow.com/questions/7391877/how-to-add-checkboxes-to-jtable-swing check this out – LordAnomander Jan 13 '16 at 11:46
  • Compare your implementation of `getColumnClass()` to [this](http://stackoverflow.com/a/7392163/230513), cited by @LordAnomander. – trashgod Jan 13 '16 at 12:01
  • I tried adding the getcolumnclass that u refered to me but when i tried implementing it in it told me "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number", sorry i am noob at this Jtable thing would u be so kind if you could tell me how to implement it – Roy Johnson Jan 13 '16 at 13:22

0 Answers0