Hi Nguyen,
You could use ULCNumberDataType:
ULCNumberDataType ndt = new ULCNumberDataType();
ndt.setInteger(true);
ndt.setMax(9999);
ndt.setMin(0);
textField.setDataType(ndt);
However, this will do the check only when the focus is lost from the
textfield, it will not prevent the user from entering the fifth digit. For
this to happen you need to extend NumberDataType or implement a custom
datatype as shown in the snippet below.
Thanks and regards,
Janak
-----------------------------------------
Janak Mulani
email: [EMAIL PROTECTED]
url: http://www.canoo.com
Beyond AJAX - Java Rich Internet Applications
http://www.canoo.com/ulc
-----------------------------------------
________________________________________
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of nguyen duyhai
Sent: Thursday, March 06, 2008 7:31 PM
To: [EMAIL PROTECTED]
Subject: [ULC-developer] Problem custom ULCTextField
Hello all,
At now, In ULCTextField support method setDataType() for limit data.
However, I want custom ULCTextField to limit data (Ex : 4 digit, 0 decimal).
When user input value to max digits (4), user can't input more digits.These
digit from over 4( 5, 6, 7) is ignored.
How to custom ULCTextField?
Ex : I input value 1234 in ULCTextField --> it 's ok.
When I press key 5, the value is not effect in ULCTextField. I expect to
that ULCTextField still show value = 1234
.
Thanks for your answers,
Kind regards,
Hai,
------------------------
import javax.swing.text.AttributeSet;
import com.ulcjava.base.application.AbstractAction;
import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.IAction;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCProxy;
import com.ulcjava.base.application.ULCTextField;
import com.ulcjava.base.application.datatype.IDataType;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.client.datatype.DataTypeConversionException;
import com.ulcjava.base.client.datatype.UIDataType;
import com.ulcjava.base.development.DevelopmentRunner;
public class LimitedLengthNumberStringSnippet extends AbstractApplication {
public void start() {
ULCTextField textField = new ULCTextField(20);
ULCMyDataType dt = new ULCMyDataType(4);
textField.setDataType(dt);
ULCBoxPane content = new ULCBoxPane(1, 0);
content.add(textField);
content.add(new ULCButton(new ShowValueAction(textField)));
ULCFrame frame = new ULCFrame("LimitedLengthNumberStringSnippet");
frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
frame.add(content);
frame.setVisible(true);
}
private static class ShowValueAction extends AbstractAction {
private ULCTextField fTextField;
public ShowValueAction(ULCTextField textField) {
fTextField = textField;
putValue(IAction.NAME, "Show Value");
}
public void actionPerformed(ActionEvent event) {
Object value = fTextField.getValue();
System.out.println("value = " + value);
System.out.println("value.getClass() = " + (value == null ? null
: value.getClass().getName()));
}
}
// server side class
public static class ULCMyDataType extends ULCProxy implements IDataType
{
private int fMaxLength;
public ULCMyDataType(int maxLength) {
fMaxLength = maxLength;
}
protected void uploadStateUI() {
super.uploadStateUI();
setStateUI("maxLength", fMaxLength);
}
protected String typeString() {
return UIMyDataType.class.getName();
// return the fully qualified name string of the UIMyDataType class, e.g.,
"com.myulcapp.client.UIMyDataType"
}
}
// client side class should be on the path of the ULC client
public static class UIMyDataType extends UIDataType {
private int fMaxLength;
public void setMaxLength(int maxLength) {
fMaxLength = maxLength;
}
public Object convertToObject(String newString, Object
previousValue) throws DataTypeConversionException {
return newString == null || newString.trim().length() == 0 ?
null : new String(newString.trim());
}
public String convertToString(Object object, boolean forEditing) {
return object == null ? null : object.toString();
}
public String filterInput(int position, String newString, String
currentText, AttributeSet attribute) {
if (newString.length() + currentText.length() > fMaxLength) {
return null;
}
for (int i = 0; i < newString.length(); i++) {
if (!Character.isDigit(newString.charAt(i))) {
return null;
}
}
return newString;
}
}
public static void main(String[] args) {
DevelopmentRunner.setApplicationClass(LimitedLengthNumberStringSnippet.class
);
DevelopmentRunner.main(args);
}
}
_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer