java允许输入table_java – 如何将JTable单元格输入标记为无效?

private static class JTable.GenericEditor使用内省来捕获通过使用无效的String值构造特定Number子类引发的异常.如果您不需要这样的通用行为,请考虑将ActiveIntegerCellEditor创建为

DefaultCellEditor的子类.您的stopCellEditing()方法将相应更简单.

附录:已更新以使用RIGHT对齐方式和常见错误代码.

private static class PositiveIntegerCellEditor extends DefaultCellEditor {

private static final Border red = new LineBorder(Color.red);

private static final Border black = new LineBorder(Color.black);

private JTextField textField;

public PositiveIntegerCellEditor(JTextField textField) {

super(textField);

this.textField = textField;

this.textField.setHorizontalAlignment(JTextField.RIGHT);

}

@Override

public boolean stopCellEditing() {

try {

int v = Integer.valueOf(textField.getText());

if (v < 0) {

throw new NumberFormatException();

}

} catch (NumberFormatException e) {

textField.setBorder(red);

return false;

}

return super.stopCellEditing();

}

@Override

public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column) {

textField.setBorder(black);

return super.getTableCellEditorComponent(

table,value,isSelected,row,column);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值