Bom dia pessoal,
Tenho um TableModel para uma tabela onde sua primeira coluna é um checkbox…
Até ai tudo certo … ele aparece certinho e ao clicar ele marca ou desmarca …
Segue o código do tablemodel:
[code]public class AtividadeOsTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private String[] nomesColunas = {"", "Código", "Descrição da Atividade", "Ultima Prev.",
"Data Execução", "Próxima Prev.", "Status"};
private Object[] objeto;
private List<Object[]> listaObjetos;
public AtividadeOsTableModel() {
super();
if (listaObjetos == null)
listaObjetos = new ArrayList<Object[]>();
}
public AtividadeOsTableModel(List<Object[]> listaObject){
super();
listaObjetos = new ArrayList<Object[]>(listaObject);
}
public int getRowCount() {
return this.listaObjetos.size();
}
public int getColumnCount() {
return this.nomesColunas.length;
}
public Object getValueAt(int row, int col) {
if (col == 0) {
return (Boolean) listaObjetos.get(row)[col];
} else if (col >= 1 && col <= 6) {
return listaObjetos.get(row)[col];
} else {
return null;
}
}
@Override
public void setValueAt(Object value, int row, int col) {
if (col == 0) {
listaObjetos.get(row)[col] = (Boolean) value;
} else if (col >= 1 && col <= 6) {
listaObjetos.get(row)[col] = value;
}
fireTableDataChanged();
}
@Override
public String getColumnName(int col){
return this.nomesColunas[col];
}
@Override
public Class<?> getColumnClass(int col){
if (col == 0) {
return Boolean.class;
} else if (col >= 1 && col <= 6) {
return String.class;
} else {
return null;
}
}
@Override
public boolean isCellEditable(int row, int col){
if (col == 0)
return true;
else
return false;
}
public void adicionaLinhaEmBranco(){
Object[] obj = new Object[] {Boolean.FALSE, "", "", "", "", "", ""};
listaObjetos.add(obj);
fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
}
public void adiciona(Object[] obj){
listaObjetos.add(obj);
fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
}
public void adicionaLista(List<Object[]> listaObj){
limpar();
int i = getRowCount();
this.listaObjetos.addAll(listaObj);
fireTableRowsInserted(i, i + getRowCount() - 1);
}
public void remove(int indice){
listaObjetos.remove(indice);
fireTableRowsDeleted(indice, indice);
}
public int getIndice(Object[] obj){
return listaObjetos.indexOf(obj);
}
public Object[] getObjeto(int indice){
return listaObjetos.get(indice);
}
public void setObjeto(Object[] obj){
objeto = obj;
listaObjetos.add(objeto);
}
public void limpar(){
if (listaObjetos != null && !listaObjetos.isEmpty()){
int i = getRowCount();
listaObjetos.clear();
fireTableRowsDeleted(0, i - 1);
}
}
}
[/code]
Ok … então meu problema é o seguinte…
quero pegar o índice da linha que o usuário digitou com a função:
porém se eu utilizo a função isCellEditable como está abaixo ele seleciona o checkbox mas retorna a linha como -1
@Override
public boolean isCellEditable(int row, int col){
if (col == 0)
return true;
else
return false;
}
e se eu usar a função desta maneira: ele traz o valor da linha como 0 por exemplo mas não seleciona o checkbox…
@Override
public boolean isCellEditable(int row, int col){
return false;
}
como eu posso fazer para que seja selecionado o checkbox e ao mesmo tempo retornar o índice da linha selecionada corretamente???
Desde já grato