Note that there are some explanatory texts on larger screens.

plurals
  1. POJava TableModel implementation crashes the program
    text
    copied!<p>In my program I wish to show some data in a <code>JTable</code>. I have a class containing the data, so the easiest way to present it in a <code>JTable</code> seems to extend my class so that it implements <code>TableModel</code> interface and use it as a model for <code>JTable</code>. Unfortunately it does not work correctly for me. When the program is about to draw the <code>JTable</code>, a <code>NullPointerException</code> is thrown at <code>javax.swing.JTable.prepareRenderer()</code>. Why?</p> <pre><code>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&lt;TableModelListener&gt; listeners; public MyTableModel() { data = new Object[7][7]; listeners = new HashSet&lt;TableModelListener&gt;(); } @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&lt;?&gt; 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; } } </code></pre> <p>And somewhere in the program:</p> <pre><code>... JTable table = new JTable(new MyTableModel()); this.setViewportView(table); ... </code></pre> <p>Thanks in advance for any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload