在我的程序中,我希望在JTable
. 我有一个包含数据的类,因此在 a 中呈现它的最简单方法JTable
似乎扩展了我的类,以便它实现TableModel
接口并将其用作JTable
. 不幸的是,它对我来说不能正常工作。当程序即将绘制 时JTable
,aNullPointerException
被抛出javax.swing.JTable.prepareRenderer()
。为什么?
package bridgecalc;
import java.util.HashSet;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class MyTableModel extends MyDataClass implements TableModel {
private static final long serialVersionUID = 1L;
// private Object[][] data; //this is in fact declared in the superclass
private static final String[] colNames = {"bla", "blabla", "hola", "hej", "egle", "begle", "eciepecie"};
private HashSet<TableModelListener> listeners;
public MyTableModel() {
data = new Object[7][7];
listeners = new HashSet<TableModelListener>();
}
@Override
public int getColumnCount() {
return data.length;
}
@Override
public int getRowCount() {
return data[0].length;
}
@Override
public Object getValueAt(int arg0, int arg1) {
return data[arg0][arg1];
}
@Override
public void addTableModelListener(TableModelListener l) {
listeners.add(l);
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return null;
}
@Override
public String getColumnName(int columnIndex) {
return colNames[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (rowIndex == columnIndex)
return false;
else return true;
}
@Override
public void removeTableModelListener(TableModelListener l) {
listeners.remove(l);
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
data[columnIndex][rowIndex] = aValue;
}
}
在程序的某个地方:
...
JTable table = new JTable(new MyTableModel());
this.setViewportView(table);
...
提前感谢您的帮助。