I may need your help in solving an issue. I want to allow numbers only in first column in a JTable, but the row is not created yet, but it will with a click on a button.
After typing some code I get a NullPointerException when trying to compile. I think the"setCellEditor" thing is causing the problem?, is it because there are no rows in the JTable "tblBestellMaske"??
How can I solve this and finally allow numbers only in column 0 in each row
The Exception:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.DefaultCellEditor.<init>(Unknown Source)
at Hauptmaske.<init>(Hauptmaske.java:588)
at Haupt.main(Haupt.java:17)
The Code:
// Tabelle BestellMaske
tblBestellMaske = new JTable(modelBestellMaske);
// Spaltengröße und Zeilenhöhe festlegen
tblBestellMaskeSpaltenModel = tblBestellMaske.getColumnModel();
tblBestellMaskeSpaltenModel.getColumn(0).setPreferredWidth(20);
tblBestellMaskeSpaltenModel.getColumn(1).setPreferredWidth(200);
tblBestellMaskeSpaltenModel.getColumn(2).setPreferredWidth(30);
tblBestellMaskeSpaltenModel.getColumn(3).setPreferredWidth(30);
tblBestellMaske.setRowHeight(30);
// Menge, Einzelpreis und Mengenpreis werden rechtsbündig angezeigt
zentrieren(tblBestellMaske, 0);
rechtsbündig(tblBestellMaske, 2);
rechtsbündig(tblBestellMaske, 3);
TableColumn tblColumn = tblBestellMaske.getColumnModel().getColumn(0);
tblColumn.setCellEditor(new DefaultCellEditor(zelle));
tblBestellMaske.setCellSelectionEnabled(true);
zelle.addKeyListener(new KeyAdapter()
{
@Override
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
if (!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))
{
getToolkit().beep();
e.consume();
}
}
});
tblBestellMaske.setFont(new Font("Segoe UI", Font.PLAIN, 14));
tblBestellMaske.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrBestellMaske.setViewportView(tblBestellMaske);
if you may need more code, let me know, thanks in advance