The DocumentValidator and DocumentTransformer system don't work worth a
damn if you have a non-zero minimum value because you're fucked by not being able to enter, for example, "1" because it's below the minimum value even if you wanted to keep typing "100" which may be valid. So instead we don't enforce the minimum value with the validator/ transformer combo and instead make sure we're at the minimum when we lose focus. It's not perfect but there is no perfect solution when we're trying to predict human intention. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1353 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
//
|
//
|
||||||
// $Id: IntField.java,v 1.4 2003/11/25 17:54:50 eric Exp $
|
// $Id: IntField.java,v 1.5 2003/12/15 18:56:27 mdb Exp $
|
||||||
|
|
||||||
package com.samskivert.swing;
|
package com.samskivert.swing;
|
||||||
|
|
||||||
|
import java.awt.event.FocusEvent;
|
||||||
|
import java.awt.event.FocusListener;
|
||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
import com.samskivert.Log;
|
import com.samskivert.Log;
|
||||||
@@ -15,7 +17,8 @@ import com.samskivert.swing.util.SwingUtil;
|
|||||||
* specified range.
|
* specified range.
|
||||||
*/
|
*/
|
||||||
public class IntField extends JTextField
|
public class IntField extends JTextField
|
||||||
implements SwingUtil.DocumentValidator, SwingUtil.DocumentTransformer
|
implements SwingUtil.DocumentValidator, SwingUtil.DocumentTransformer,
|
||||||
|
FocusListener
|
||||||
{
|
{
|
||||||
public IntField ()
|
public IntField ()
|
||||||
{
|
{
|
||||||
@@ -32,8 +35,9 @@ public class IntField extends JTextField
|
|||||||
setHorizontalAlignment(JTextField.RIGHT);
|
setHorizontalAlignment(JTextField.RIGHT);
|
||||||
setColumns(5);
|
setColumns(5);
|
||||||
|
|
||||||
// register ourselves as the validator
|
// register ourselves as the validator and focus listener
|
||||||
SwingUtil.setDocumentHelpers(this, this, null);
|
SwingUtil.setDocumentHelpers(this, this, null);
|
||||||
|
addFocusListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -92,13 +96,24 @@ public class IntField extends JTextField
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
int value = Integer.parseInt(text);
|
int value = Integer.parseInt(text);
|
||||||
|
return (value <= _maxValue);
|
||||||
return ((value >= _minValue) && (value <= _maxValue));
|
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// documentation inherited from interface
|
||||||
|
public void focusGained (FocusEvent e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// documentation inherited from interface
|
||||||
|
public void focusLost (FocusEvent e)
|
||||||
|
{
|
||||||
|
// here we ensure that we're not below our minimum
|
||||||
|
setText(transform(getText()));
|
||||||
|
}
|
||||||
|
|
||||||
protected int _minValue;
|
protected int _minValue;
|
||||||
protected int _maxValue;
|
protected int _maxValue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user