0

I want to control user input text on jtextfield. It seems I can't find any good way in netbean 8. in C# use keypress event, but in java I'm new.

I choose key type event

I want input only number with 2 digit after decimal

10.00
1224547885544.12
545545464646464646465466.10

not

12121212.654654654654

I've tried

                 // not a good idea

       char c=evt.getKeyChar();
    if((Character.isDigit(c))||(c==KeyEvent.VK_PERIOD)||(c==KeyEvent.VK_BACK_SPACE)){
        int punto=0;
        if(c==KeyEvent.VK_PERIOD){ 
                    String s=pricefield.getText();
                    int dot=s.indexOf('.');
                    punto=dot;
                    if(dot!=-1){
                        getToolkit().beep();
                        evt.consume();
                    }
                }
    }
    else{    
        getToolkit().beep();
        evt.consume();
    }

    //second try

   char enter = evt.getKeyChar();
if(!(Character.isDigit(enter))){
    evt.consume();
}

it's not good idea I think.

try other many ways.

please, help me.

frianH
  • 7,295
  • 6
  • 20
  • 45

2 Answers2

0

Assuming you are referring to a JavaFX TextField:

You can get the textProperty of the textfield by calling textField.textProperty(). Since this is a property, you can attach a listener to it, to listen for changes to the text in the field:

textField.textProperty().addListener((observable, oldValue, newValue) -> {
    // this code is called whenever the text in the field changes
    // oldValue is the text contained before the event was triggered
    // newValue is the text that the field is about to be set to

    if (oldValue.contains("[a-zA-Z]")) {  // any predicate you want/need
        textField.setText(oldValue);  // revert the text of the field back to its old value
    }
});
cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • I want to control when user typing from keyboard direct to the textfield. user can type only number not letter or symbol but "." is allowed. and after decimal user can type only 2 digit. format like ( 1111111.11 ) – Verdone Yang Jul 31 '19 at 09:56
  • You will have to modify the `if` statement a little bit, but with a quick google of "regular expressions" this should be simple enough from this code – cameron1024 Jul 31 '19 at 10:06
0

For a Swing TextField, that should help you:

 JFormattedTextField textField = new JFormattedTextField();
        textField.setFormatterFactory(new AbstractFormatterFactory() {
            @Override
            public AbstractFormatter getFormatter(JFormattedTextField tf) {
                NumberFormat format = DecimalFormat.getInstance();
                //or two, if you want to force something like 10.00
                format.setMinimumFractionDigits(0); 
                format.setMaximumFractionDigits(2);
                format.setRoundingMode(RoundingMode.HALF_UP);
                InternationalFormatter formatter = new InternationalFormatter(format);
                formatter.setAllowsInvalid(false); //important!
                return formatter;
            }
        });
ItFreak
  • 2,299
  • 5
  • 21
  • 45