AbstractTableModel - 不返回getRowCount()

时间:2013-04-11 12:51:06

标签: java swing jtable abstracttablemodel

这里是代码,我将表模型添加到我的表中:

table = new JTable(new TerminiTableModel());
scrollPane = new JScrollPane(table);

然后我创建了这个类TerminiTableModel(),这里只是一个简单的生成代码,我想得到列cound和行计数...列计数工作正常,并添加3列到我的表(“Termini”,“ Ime我prezime“和”Napomena“)。当我在getRowCount中添加该行时,我得到错误,甚至无法编译它......这是代码...我搜索了这个并且无法弄清楚...: - /

public class TerminiTableModel extends AbstractTableModel implements TableModel {

    protected TableModel tableModel;

    private String[] columnNames = {"Termin", "Ime i prezime", "Napomena"};
    private String[] data = {"7:00", "8:00", "9:00" , "10:00" , "11:00" , "12:00" };

    @Override
    public void addTableModelListener(TableModelListener arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public Class<?> getColumnClass(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return data.length;   //  <<----- this line is problem
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

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

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col > 0) {
            return true;
        } else
        return false;
    }

    @Override
    public void removeTableModelListener(TableModelListener arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setValueAt(Object data, int row, int col) {
        // TODO Auto-generated method stub
    }
}

解决方案可能很简单,但我需要一些帮助...... 提前谢谢你......

2 个答案:

答案 0 :(得分:3)

基于通用的编辑

JTable中的

行基于两个Dimensional数组,然后是

private String[] data = {"7:00", "8:00", "9:00" , "10:00" , "11:00" , "12:00" };

应该是

private String[][] data = {{"7:00", null, null}, {"8:00", null, null}, 
            {"9:00", null, null}, {"10:00", null, null}, {"11:00", null, null}, 
            {"12:00", null, null}};

用于已定义的列

enter image description here

来自代码

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

public class MyTable {

    private JFrame frame = new JFrame("TableDemo");
    private JScrollPane tableScroll = new JScrollPane();
    private MyTableModel tableModel = new MyTableModel();
    private JTable dialogTable = new JTable(tableModel);

    public MyTable() {
        dialogTable.setPreferredScrollableViewportSize(dialogTable.getPreferredSize());
        tableScroll = new JScrollPane(dialogTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        frame.add(tableScroll);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        System.out.println(dialogTable.getRowCount());
        System.out.println(dialogTable.getColumnCount());
        System.out.println(tableModel.getRowCount());
        System.out.println(tableModel.getColumnCount());
    }

    private class MyTableModel extends AbstractTableModel {

        private String[] columnNames = {"Termin", "Ime i prezime", "Napomena"};
        private String[][] data = {{"7:00", null, null}, {"8:00", null, null}, 
            {"9:00", null, null}, {"10:00", null, null}, {"11:00", null, null}, 
            {"12:00", null, null}};

        @Override
        public void addTableModelListener(TableModelListener arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public Class<?> getColumnClass(int columnNames) {
            switch (columnNames) {
                case 0:
                    return String.class;//return Date.class;
                case 1:
                    return String.class;//return Double.class;
                case 2:
                    return String.class;//return Boolean.class;
                default:
                    return String.class;
            }
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public int getRowCount() {
            return data.length;   //  <<----- this line is problem
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

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

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col > 0) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void removeTableModelListener(TableModelListener arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void setValueAt(Object data, int row, int col) {
            // TODO Auto-generated method stub
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyTable myTable = new MyTable();
            }
        });
    }
}

答案 1 :(得分:1)

导致的错误是:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

由未指定列类引起:

@Override
public Class<?> getColumnClass(int arg0) {
   return String.class;
}

getValueAt也不会返回任何数据。您正在使用一维数组来存储数据,假设JTable数据以行和数字表示。您需要使用2D数组的列。