Check in swt text

I would like to confirm the input of numbers in the text box.

I want the user to enter only an integer, a decimal number in the field between the maximum and minimum values.

How can I be sure of this?

Thanks.

+6
source share
7 answers

Use VerifyListener since this will handle insert, backspace, replace .....

eg.

 text.addVerifyListener(new VerifyListener() { @Override public void verifyText(VerifyEvent e) { final String oldS = text.getText(); final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end); try { BigDecimal bd = new BigDecimal(newS); // value is decimal // Test value range } catch (final NumberFormatException numberFormatException) { // value is not decimal e.doit = false; } } }); 
+11
source

You can register the ModifyListener with a text control and use it to check the number.

  txt.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent event) { String txt = ((Text) event.getSource()).getText(); try { int num = Integer.parseInt(txt); // Checks on num } catch (NumberFormatException e) { // Show error } } }); 

You can also use addVerifyListener to prevent certain characters from being entered. In the case that passed into this method, there is a "doit" field. If you set it to false, this will prevent the current change.

+3
source

Integer check in SWT

 text = new Text(this, SWT.BORDER); text.setLayoutData(gridData); s=text.getText(); this.setLayout(new GridLayout()); text.addListener(SWT.Verify, new Listener() { public void handleEvent(Event e) { String string = e.text; char[] chars = new char[string.length()]; string.getChars(0, chars.length, chars, 0); for (int i = 0; i < chars.length; i++) { if (!('0' <= chars[i] && chars[i] <= '9')) { e.doit = false; return; } } } }); 
+3
source

try the following code:

 /** * Verify listener for Text */ private VerifyListener verfyTextListener = new VerifyListener() { @Override public void verifyText(VerifyEvent e) { String string = e.text; Matcher matcher = Pattern.compile("[0-9]*+$").matcher(string); if (!matcher.matches()) { e.doit = false; return; } } }; 
0
source
  • If you want to allow only Integer values, you can use Spinner instead of a text field.

  • To check double values, you should take into account that type E characters can be included in double digits, and partial input, for example “4e-”, must be valid when typing. Such partial expressions will give a NumberFormatException for Double.parseDouble (partialExpression)

  • Also see my answer on the following related question: How to set a mask in SWT text to allow only Decimals

0
source

1. Create a utility to verify utill execution.

2. Verification Authentication Method

3. realize your logic

4. create an object of the utill class in which you want to use a test listener (in the text)

5.text.addverifylistener (utillclassobject)

Example: - 1. use the class: -

 public class UtillVerifyListener implements VerifyListener { @Override public void verifyText(VerifyEvent e) { // Get the source widget Text source = (Text) e.getSource(); // Get the text final String oldS = source.getText(); final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end); try { BigDecimal bd = new BigDecimal(newS); // value is decimal // Test value range } catch (final NumberFormatException numberFormatException) { // value is not decimal e.doit = false; } } 

2. using verifylistener in another class

 public class TestVerifyListenerOne { public void createContents(Composite parent){ UtillVerifyListener listener=new UtillVerifyListener(); textOne = new Text(composite, SWT.BORDER); textOne .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); textOne .addVerifyListener(listener) textTwo = new Text(composite, SWT.BORDER); textTwo .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); textTwo .addVerifyListener(listener); 

}}

0
source
 text.addVerifyListener(new VerifyListener() { @Override public void verifyText(VerifyEvent e) { Text text = (Text) e.getSource(); final String oldS = text.getText(); String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end); boolean isValid = true; try { if(! "".equals(newS)){ Float.parseFloat(newS); } } catch (NumberFormatException ex) { isValid = false; } e.doit = isValid; } }); 
0
source

Source: https://habr.com/ru/post/910567/


All Articles