If I take a JTable
and specify a column's classtype on it's model as follows:
DefaultTableModel model = new DefaultTableModel(columnNames, 100) {
@Override
public Class<?> getColumnClass(int columnIndex) {
return Integer.class;
}};
Then whenever a user tries to enter a double
value into the table, Swing automatically rejects the input and sets the cell's outline to red.
I want the same effect to occur when someone enters a 'negative or 0' input to the cell. I've got this:
@Override
public void setValueAt(Object val, int rowIndex, int columnIndex) {
if (val instanceof Number && ((Number) val).doubleValue() > 0) {
super.setValueAt(val, rowIndex, columnIndex);
}
}
}
This prevents the cell from accepting any non-positive values, but it doesn't set the color to red and leave the cell as editable.
I tried looking into how JTable's doing the rejection by default, but I can't seem to find it.
How can I make it reject the non-positive input the same way it rejects the non-Integer input?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…