简体   繁体   English

来自AbstractTableModel的JTable不显示

[英]JTable from AbstractTableModel not showing

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. 我在eclipse中使用Window Builder插件来制作所有可视组件,我添加了一个JTable,起初它是一个简单的JTable,并且可以正确显示数据。 Since i need that the data of the JTable be non-editable, i create a model to use the method isCellEditable. 由于我需要JTable的数据不可编辑,因此我创建了一个模型来使用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. 它工作正常,我不知道该错误是否与de Window Builder形式的日蚀有关,还是代码中导致我新使用JTables。

your issue is return 0; 您的问题是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 请改用DefaultTableModel ,确定是否对Object[][]数据或Vector of Vectors使用AbstractTableModel并不是真正重要的问题

usage of AbstractTableModel make me sence for model based on HashMap or java.util.List ei 使用AbstractTableModel可以使基于HashMapjava.util.List ei的模型成为现实

Your AbstractTableModel does not contain any data. 您的AbstractTableModel不包含任何数据。

You indicate it contains zero rows and zero columns by your implementation of the getColumnCount and getRowCount methods. 通过实现getColumnCountgetRowCount方法,可以表明它包含零行和零列。

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. 即使调整这些方法,也不会使用在构造函数中传递的数据,因为您总是在getValueAt方法中返回null

I think you should start reading the 'How to create a TableModel ' section from the table tutorial 我认为您应该开始阅读表格教程中的“如何创建TableModel部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM