Leave only one editable Jtable column

Asked

Viewed 731 times

0

I have my data model that I made for tests as shown in the code below:

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 set up a class with the 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 click that fills the JTable, with test values.

inserir a descrição da imagem aqui

The event I created was like this

ModeloDados d1 = new ModeloDados("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, for example the last one that would be for the user to edit the phone.
However I’m not getting to leave the editable column will someone help me in this?

1 answer

1


Taking your code as the 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 will also need to implement the method setValueAt() inherited from the class AbstractTableModel.

If you have doubts about the implementation, in this question there are complete implementation instructions for a proprietary tablemodel.


The setValueAt() could be 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();
}
  • I needed to leave dynamic this isCellEditabel, so when called just pass the column to make it editable.

  • @Valdecir did not understand your doubt, how so dynamite? In the question you say you need to leave the last editable column, so it will be editable.

  • yes, but this class is a test class... Suppose you would need to leave other editable tables in different columns

  • @Valdecir this comment presents a different doubt than the one asked. You asked how to leave only one editable column, and gave as an example the third column of a table with 3. The answer is precisely for this problem presented in the question. And for this other question, you want to apply the class to different tables, it is not interesting to do this, besides being complicated, easier to create a model for each one and treat the columns as the answer guided.

  • I get it, it really gets better to do specific... I’m doing exactly as above but I couldn’t implement setValueAt(), could give me a strength?

  • @Valdecir I can, but the code of the linked question at the end did not help you? There is a complete code and ready to exemplify, and has the implementation of the tablemodel with this method, if even that code does not help, comments here the problem you have facing when implementing.

  • So I’m new to this jTable scheme, got her popular fully and this editable. However I have to implement setValueAt() so that the editions are accepted. The linked explanation creates an employee type to enter the data and I am working different step an array of objects as it should proceed?

  • @Valdecir tries as follows: http://pastebin.com/5b1G41Kk

  • 1

    Perfect... Thanks Help!!!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.