0

Im using Window Builder plugin in eclipse to make all the visual components, and i add a JTable, and at first it was a simple JTable, and it show the data correctly. Since i need that the data of the JTable be non-editable, i create a model to use the method isCellEditable. my code is this.

public class MyTableModel extends AbstractTableModel {

        private static final long serialVersionUID = 1L;
        private String[] columnNames;
        private Object[][] data;

        public MyTableModel(Object[][] sentdata, String[] cnames){
            columnNames = cnames;
            data = sentdata;
        }

        @Override
        public int getColumnCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getRowCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            // TODO Auto-generated method stub
            return null;
        }

        public boolean isCellEditable (int row, int column){
            return false;
        }

    }//fin del modelo

and in the constructor of the class is this

String[] NombresdeColumnas = {"Nombre del Producto", "Cantidad en Inventario", "Precio Unitario"}; 
RegistroInventario inventariodatos = new RegistroInventario();
Object[][] data = inventariodatos.regresarInventario();

MyTableModel model1 = new MyTableModel(data, NombresdeColumnas);

Table_Inventario = new JTable(model1);
Table_Inventario.setGridColor(Color.gray);

JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setViewportView(Table_Inventario);

and it doesnt show anything, but when i was doing this:

Table_Inventario = new JTable(data, NombresdeColumnas);

it was working just fine, i don´t know if the error has todo with de Window Builder form eclipse or in the code cause im new doning JTables.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Pedro
  • 41
  • 9

2 Answers2

2

your issue is return 0; in

public int getColumnCount() {

and

public int getRowCount() {

use DefaultTableModel instead, sure if isn't there really important issue to use AbstractTableModel for Object[][] data or Vector of Vectors

usage of AbstractTableModel make me sence for model based on HashMap or java.util.List e.i.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • thanks man i thought does methods were not taht important, i thing a will search a little more in detail about the AbstractTableModel. – Pedro Oct 28 '12 at 19:04
  • [can start with](http://stackoverflow.com/questions/6900628/refreshing-background-color-for-a-row-in-jtable/6901508#6901508) – mKorbel Oct 28 '12 at 19:07
2

Your AbstractTableModel does not contain any data.

You indicate it contains zero rows and zero columns by your implementation of the getColumnCount and getRowCount methods.

And even when you adjust those methods, you do not use the data passed in the constructor as you always return null in the getValueAt method.

I think you should start reading the 'How to create a TableModel' section from the table tutorial

Robin
  • 36,233
  • 5
  • 47
  • 99