2

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

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

然后我做了这个类TerminiTableModel(),这只是一个简单的生成代码,我想在其中获取列数和行数......列数工作得很好,并将3列添加到我的表中(“Termini”,“Ime i prezime ”和“纳波梅纳”)。当我在 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
    }
}

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

4

2 回答 2

3

编辑基于commnets

JTable 中的 rows 基于二维数组,那么

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}};

对于定义的列

在此处输入图像描述

从代码

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();
            }
        });
    }
}
于 2013-04-11T13:03:01.633 回答
1

造成的错误是这样的:

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

这是由于未指定列类引起的:

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

也不getValueAt返回任何数据。您正在使用一维数组来存储数据,鉴于JTable数据以行和列表示,您需要使用二维数组。

于 2013-04-11T13:03:07.940 回答