import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TextNumeric
{
Text text;
Label message;
public TextNumeric() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
shell.setSize(300,160);
text = new Text(shell, SWT.SINGLE | SWT.BORDER);
message = new Label(shell, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 1;
message.setLayoutData(gridData);
ModifyListener listener = new ModifyListener() {
public void modifyText(ModifyEvent e) {
valueChanged((Text) e.widget);
}
};
text.addModifyListener(listener);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public void valueChanged(Text textfield) {
if (!text.isFocusControl())
return;
if (textfield == text) {
try {
double fValue = Double.parseDouble(text.getText());
message.setText("this is number format: "+fValue);
} catch(NumberFormatException e) {
text.setText("");
message.setText("Invalid number format: " + text.getText());
}
}
}
public static void main(String[] args) {
TextNumeric tn = new TextNumeric();
}
}