gpt4 book ai didi

java - 将复选框添加到 jTable?

转载 作者:行者123 更新时间:2023-11-30 06:13:01 24 4
gpt4 key购买 nike

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

我正在使用我在互联网上获得的自己的表格模型。

表模型的代码:

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

我可以使用哪个代码?我该把这段代码放在哪里??

最佳答案

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

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

关于java - 将复选框添加到 jTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32255379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com