I have a table (MyTable) with a tablemodel and a DefaultCellEditor (see below code at the bottom). I do not want any cells in my table to be negative but empty cells are acceptable. When a user deletes the content of a cell, an error is triggered here:
if(txtfld.getText().isEmpty()==true)
return super.stopCellEditing(); //this triggers an error
What do I need to change in the code to be able to have nulls in the table?
Thanks in advance
--------------------------------------------------------
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class MyFrame extends javax.swing.JFrame {
public MyFrame() {
initComponents();
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
MyTable = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
MyTable.setModel(new MyTableModel());
MyTable.setDefaultEditor(Double.class,new TableCellNumericEditor(new javax.swing.JTextField()));
jScrollPane1.setViewportView(MyTable);
getContentPane().add(jScrollPane1);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
}
private javax.swing.JTable MyTable;
private javax.swing.JScrollPane jScrollPane1;
}
class MyTableModel extends AbstractTableModel{
private String[] columnNames = {"A","B","C","D","E"};
private Object[][] data = {
{10,25.8,29.6,33.0,36.1},
{17.5,20.9,24.0,26.7,45},
};
public boolean isCellEditable(int rowIndex,
int columnIndex){
return true;
}
public Class<?> getColumnClass(int columnIndex) {
//return data[0][columnIndex].getClass();
return Double.class;
}
public int getRowCount(){
return data.length;
}
public int getColumnCount(){
return data[0].length;
}
public Object getValueAt(int row,int column){
return data[row][column];
}
public void setValueAt(Object aValue, int row, int column) {
data[row][column]=aValue;
}
public String getColumnName(int c){
return columnNames[c];
}
}
class TableCellNumericEditor extends DefaultCellEditor implements TableCellEditor {
private static final Border redcol = new LineBorder(Color.red);
private static final Border blackcol = new LineBorder(Color.black);
private JTextField txtfld;
public TableCellNumericEditor(JTextField txtfld) {
super(txtfld);
this.txtfld = txtfld;
this.txtfld.setHorizontalAlignment(JTextField.CENT ER);
}
public boolean stopCellEditing() {
if(txtfld.getText().isEmpty()==true)
return super.stopCellEditing(); //this triggers an error
try {
double vald = Double.valueOf(txtfld.getText());
if (vald < 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
txtfld.setBorder(redcol);
return false;
}
return super.stopCellEditing();
}
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
txtfld.setBorder(blackcol);
return super.getTableCellEditorComponent(
table, value, isSelected, row, column);
}
}