Java – how to mark JTable cell input as invalid?

If I use JTable and specify the classtype of the column on its model, it is as follows: @ h_ 502_ 2@DefaultTableModel model = new DefaultTableModel(columnNames,100) { @Override public Class getColumnClass(int columnIndex) { return Integer.class; }};

Then, whenever the user tries to enter a double value in the table, swing automatically rejects the input and sets the outline of the cell to red

When someone enters "negative or 0" into a cell, I want the same effect I have this:

This prevents the cell from accepting any non positive values, but does not set the color to red and leaves the cell editable

I tried to see how JTable rejects by default, but I couldn't seem to find it

How to reject non positive input in the same way as rejecting non integer input?

thank you

Solution

private static class JTable. Genericeditor uses introspection to catch exceptions thrown by constructing a specific number subclass with an invalid string value If you do not need such a generic behavior, consider creating activeintegercelleditor as a subclass of defaultcelleditor Your stopcellediting () method will be simpler accordingly

Appendix: updated to use right alignment and common error codes

Appendix: See also using an editor to validate user entered text

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>