1

I have a subclass of AbstractTableModel and a JFrame to display the data from my table, ran over and the only error that appears instead of the column names appear A, B, C

What am I doing wrong?

Here are my classes

    package Biblioteca;

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

public class TabelaAlunos extends AbstractTableModel {

    private ArrayList linhas = null;
    private String[] colunas = {"id_aluno", "nome_aluno", "matricula", "telefone", "email", "sexo"};

    public TabelaAlunos(ArrayList lin, String[] col) {

        setColunas(col);
        setLinhas(lin);

    }

    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() {
        return colunas.length;
    }
@Override
    public int getRowCount() {

        return linhas.size();
    }
 @Override
    public String getColumnName(int columnIndex) {
        return colunas[columnIndex];

    }
@Override
    public Object getValueAt(int numLin, int columnIndex) {

        Object[] linha = (Object[]) getLinhas().get(numLin);
        return linha[columnIndex];
    }

};

1 Answers1

2

There is a typo in this method:

    public String getColomnName(int numCol) {
        return colunas[numCol];
    }

It should be (note the u instead of o):

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

This is why @Override annotation is important when we are subclassing and overriding methods. If you include this annotation in your actual code it shouldn't compile because getColomnName(...) is not defined in parent class.

The same principle applies for all these methods:

  • getColumnCount()
  • getRowCount()
  • getValueAt(int row, int column)

Edit

Based on your update your table model looks just fine. I've made an MCVE using your table model and it all worked as expected. Check out the questions' third revision, you don't use TabelaAlunos table model but TabelaLivros instead, so it's probably the issue is in that table model.

You might also consider wrap your data using POJO's to model business data and implement a table model like exemplified here. There are advanced alternatives shown here and here. Also see Table From Database by Rob Camick.

Finally, please see the example below:

import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;


public class Demo {

    private void createAndShowGUI() {

        String[] header = {"id_aluno", "nome_aluno", "matricula", "telefone", "email", "sexo"};
        ArrayList<Object[]> data = new ArrayList<>();
        data.add(new Object[] {1, "Fernando", "1234567890", "1234-567890", "email@example.com", "M"});

        TableModel model = new TabelaAlunos(data, header);
        JTable table = new JTable(model);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().createAndShowGUI();
            }
        });
    }

    public class TabelaAlunos extends AbstractTableModel {

        private ArrayList linhas;
        private String[] colunas;

        public TabelaAlunos(ArrayList lin, String[] col) {
            setColunas(col);
            setLinhas(lin);
        }

        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() {
            return colunas.length;
        }

        @Override
        public int getRowCount() {

            return linhas.size();
        }
        @Override
        public String getColumnName(int columnIndex) {
            return colunas[columnIndex];

        }

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

Screenshot

Example

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • Already did it, but always appear in A, B,C,D in name columns – Fernando Tricks Combo Nov 12 '14 at 16:11
  • That outcome suggests that the default implementation of `getColumnName(int columnIndex)` inherited from `AbstractTableModel` is the one being called, not yours. So I guess you still having issues on overriding that method correctly in order to return the approapriate column name. Please [edit](http://stackoverflow.com/posts/26890337/edit) your question with your table model code updated. @FernandoTricksCombo – dic19 Nov 12 '14 at 21:03
  • like this? but the name remains the same – Fernando Tricks Combo Nov 16 '14 at 17:43
  • Please see my edit. Hopefully you can make it work now @FernandoTricksCombo – dic19 Nov 17 '14 at 11:42
  • You are welcome. If the answer helped you to solve the problem pelase don't forget to [mark it as accepted](http://stackoverflow.com/help/accepted-answer). @FernandoTricksCombo – dic19 Nov 20 '14 at 13:17