0

我很困惑,我已经像 20 次一样重新设计了我的表格模型。我开始认为我的问题可能不在我的表格模型或数据源中,而是在我的 MainView 中。我以前从未使用过标签窗格......也许它不喜欢标签?最后提到了我的 mainView 代码。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)

我的表模型

public class MyTable implements TableModel{

ArrayList<Inventory> inventoryList;
MyDataSource source = new MyDataSource();

String[] columnNames = {"ID","Product","Price","Instock","Sold"};

public MyTable(){

    inventoryList = source.getInventory();
}

@Override
public void addTableModelListener(TableModelListener l) {

}

@Override
public Class<?> getColumnClass(int columnIndex) {

    return null;
}

@Override
public int getColumnCount() {
    return 5;
}

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

@Override
public int getRowCount() {
    return inventoryList.size();
}

@Override
public Object getValueAt(int row, int col) {

    Inventory inventory = inventoryList.get(row);
    switch(col){
    case 0: return inventory.getID();
    case 1: return inventory.getProductName();
    case 2: return inventory.getPrice();
    case 3: return inventory.getInstock();
    case 4: return inventory.getSold();
    }

    return null;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {

    return false;
}

@Override
public void removeTableModelListener(TableModelListener l) {

}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

}

}

我的主要课程

        JScrollPane scrollPane = new JScrollPane();
    tabbedPane.addTab("Database", null, scrollPane, null);

    table = new JTable(tableModel);
    scrollPane.setViewportView(table);      
4

2 回答 2

4

你的错误在这里

@Override
public Class<?> getColumnClass(int columnIndex) {
    return null;
}

您正在为表使用默认渲染器,因此您必须指定列类。但是你返回null而不是那个,尝试用它替换它String.class,它会起作用。

于 2013-10-30T18:49:52.720 回答
2

您将希望扩展 AbstractTableModel,而不仅仅是实现 TableModel,否则您的模型将无法正常工作,包括所有 fireXXX 方法。

于 2013-10-30T18:42:53.000 回答