将CheckBox添加到jTable?

时间:2015-08-27 17:00:03

标签: java swing jdbc netbeans jtable

我需要在行中添加一个复选框,但我不知道该怎么做。

我正在使用我在互联网上获得的自有桌面模型。

表模型的代码:

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/**************/

public class ModeloTabela extends AbstractTableModel {
    private ArrayList linhas = null;
    private String [] colunas = null;

    public ModeloTabela(ArrayList lin, String[] col){
        setLinhas(lin);
        setColunas(col);
    }
    public ArrayList getLinhas(){
        return linhas;

    }
    public void setLinhas(ArrayList dados){
        linhas = dados;
    }
    public String[] getColunas(){
        return colunas;
    }
    public void setColunas(String [] nomes){
        colunas = nomes;
    }
    public int getColumnCount(){
        return colunas.length;
    }
    public int getRowCount(){
        return linhas.size();
    }
    public String getColumnCount (int numCol){
        return colunas[numCol];
    }

    public Object getValueAt(int numLin, int numCol){
        Object[] linha = (Object[])getLinhas().get(numLin);
        return linha[numCol];
    }

此方法将填写表格:

public void PreencheTabela(String sql, JTable jt1, JTextField j1){
        c.conexao();
        ArrayList dados = new ArrayList();

        String [] Colunas = new String[] {"STATUS", "ID", "FABRICANTE", "FORNECEDOR", "NOME", "CATEGORIA", "DATA DO CADASTRO", " CODIGO DE BARRAS", "QUANTIDADE"};

        c.executaSQL(sql);
        try { 
            c.rs.first();
            do{
            dados.add(new Object[]{c.rs.getInt("prod_id"),c.rs.getString("prod_fabricante"),c.rs.getString("prod_fornecedor"),c.rs.getString("prod_nome"),
                c.rs.getString("prod_categoria"),c.rs.getDate("prod_datacadastro"),c.rs.getString("prod_codbarras"), c.rs.getInt("est_quantidade")});
            j1.setBorder(BorderFactory.createLineBorder(Color.black));
        }while(c.rs.next());

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro!\n"+ex.getMessage());
        }
        ModeloTabela modelo = new ModeloTabela(dados, Colunas);
        jt1.setModel(modelo);
}

我可以使用哪些代码?我在哪里放这个代码??

1 个答案:

答案 0 :(得分:5)

您需要在TableModel中实现getColumnClass(int columnIndex),并确保它将为您要包含的列复选框返回布尔值。

看一下本指南,它非常有用: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data