Leave only one column of the editable JTable

0

I have my data model that I did for testing as the code below shows:

public class ModeloDados {

    private String nome;
    private String sobreNome;
    private String telefone;

    public ModeloDados(String nome, String sN, String fn){
        this.nome=nome;
        this.sobreNome=sN;
        this.telefone=fn;
    }

    /**
     * @return the nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    /**
     * @return the sobreNome
     */
    public String getSobreNome() {
        return sobreNome;
    }

    /**
     * @param sobreNome the sobreNome to set
     */
    public void setSobreNome(String sobreNome) {
        this.sobreNome = sobreNome;
    }

    /**
     * @return the telefone
     */
    public String getTelefone() {
        return telefone;
    }

    /**
     * @param telefone the telefone to set
     */
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

}

And I also put a class with AbstractTableModel , which would be the table template:

public class tabelaPrincipal extends AbstractTableModel {

    private ArrayList linhas = null;
    private String[] colunas = null;

    public tabelaPrincipal(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;
    }

    @Override
    public int getColumnCount() {
        //retorna a quantidade de colunas(conta a quantidade e retorna)
        return colunas.length;
    }

    @Override
    public int getRowCount() {
        //retorna o tamanho do array(quantos letras tem)
        return linhas.size();
    }

    @Override
    public String getColumnName(int numCol) {
        return colunas[numCol];
    }

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


    public boolean isCellEditabel(int rowIndex, int columnIndex){
        return true;
    }
}

And within a Form I created an event that clicked on that fills the JTable , with test values.

TheeventIcreatedwaslikethis

ModeloDadosd1=newModeloDados("Valdecir", "Padovani", "AAAA");
    ModeloDados d2 = new ModeloDados("João", "Silva", "BBBB");
    ModeloDados d3 = new ModeloDados("Jose","Martins","CCCC");

    ArrayList array = new ArrayList<>();

    array.add(new Object[]{d1.getNome(),d1.getSobreNome(),d1.getTelefone()});
    array.add(new Object[]{d2.getNome(),d2.getSobreNome(),d2.getTelefone()});
    array.add(new Object[]{d3.getNome(),d3.getSobreNome(),d3.getTelefone()});

    String[] colunas = {"NOME","SOBRE NOME","NUMERO"};

    tabelaPrincipal modeloTabela = new tabelaPrincipal(array, colunas);
    jTable1.setModel(modeloTabela);

I would like to leave an editable column, such as the last one it would be for the user to edit the phone. But I can not make the column editable, can anyone help me with this?

    
asked by anonymous 21.07.2016 / 15:00

1 answer

1

Taking your code as a starting point, where the table appears to have only 3 columns, change your method this way:

public boolean isCellEditabel(int rowIndex, int columnIndex){
    return columnIndex == 2;
}

Remembering that you also need to implement the setValueAt() inherited from class AbstractTableModel .

If you have questions about the implementation, in this question there are complete instructions for implementing a tablemodel itself.

The setValueAt() could look something like this:

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

   Object[] linha = (Object[]) getLinhas().get(rowIndex);
   linha[columnIndex] = aValue;
   //este método é quem notifica a mudança do model na tabela
   fireTableDataChanged();
}
    
21.07.2016 / 15:02