Hello Janak
Thanks for your answers,
Best regards,
Nguyen.
---------------------------------------------------
Hi Nguyen,
You could use ULCNumberDataType:
ULCNumberDataType ndt =3D new ULCNumberDataType();
ndt.setInteger(true);
ndt.setMax(9999);
ndt.setMin(0);
=20
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
-----------------------------------------=20
________________________________________
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,=20
At now, In ULCTextField support method setDataType() for limit data.
However, I want custom ULCTextField to limit data (Ex :=A04 digit,=A00 =
decimal).
When user input value to max=A0digits (4), user can't input =
more=A0digits.These
digit from=A0over 4( 5, 6, 7) =A0is ignored.
How to custom ULCTextField?
Ex : I input value 1234 in ULCTextField --> it 's ok.
=A0When I press key 5, the value is not effect in ULCTextField. I expect =
to
that ULCTextField=A0still show value =3D 1234
.
Thanks for your answers,
=A0
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 =3D new ULCTextField(20);
ULCMyDataType dt =3D new ULCMyDataType(4);
textField.setDataType(dt);
ULCBoxPane content =3D new ULCBoxPane(1, 0);
content.add(textField);
content.add(new ULCButton(new ShowValueAction(textField)));
ULCFrame frame =3D 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 =3D textField;
putValue(IAction.NAME, "Show Value");
}
public void actionPerformed(ActionEvent event) {
Object value =3D fTextField.getValue();
System.out.println("value =3D " + value);
System.out.println("value.getClass() =3D " + (value =3D=3D =
null ? null
:* value.getClass().getName()));
* }
}
// server side class
public static class ULCMyDataType extends ULCProxy implements =
IDataType
{
private int fMaxLength;
public ULCMyDataType(int maxLength) {
fMaxLength =3D maxLength;
}
protected void uploadStateUI() {
super.uploadStateUI();
setStateUI("maxLength", fMaxLength);
}
protected String typeString() {
return UIMyDataType.class.getName();=20
// 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 =3D maxLength;
}
public Object convertToObject(String newString, Object
previousValue) throws DataTypeConversionException {
return newString =3D=3D null || newString.trim().length() =
=3D=3D 0 ?
null : new String(newString.trim());
}
public String convertToString(Object object, boolean forEditing) =
{
return object =3D=3D 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 =3D 0; i < newString.length(); i++) {
if (!Character.isDigit(newString.charAt(i))) {
return null;
}
}
return newString;
}
}
public static void main(String[] args) {
=20
DevelopmentRunner.setApplicationClass(LimitedLengthNumberStringSnippet.cl=
ass
);
DevelopmentRunner.main(args);
}
}