JTable中的CheckBox

时间:2016-01-13 11:40:40

标签: java swing checkbox jtable

我无法在JTable内部制作清单,因为您可以在图片中看到它在" Checked"我希望将该列设为复选框。我查了here看看如何做清单,但我不知道从哪里开始。

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

}

0 个答案:

没有答案