AbstractTableModel Help in name Columns sql

208 views Asked by At

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

There are 1 answers

5
dic19 On BEST ANSWER

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 protected]", "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