Disable input some symbols to JTextField

前端 未结 7 803
长发绾君心
长发绾君心 2020-12-05 21:07

How can I disable input of any symbol except digits to JTextField?

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 21:44

    For a better user experience

    Others have mentioned the use of JFormattedTextField or KeyListeners to prevent invalid data from being entered but from a usability point of view I find it very annoying to start typing into a field and nothing happens.

    To provide a better user experience you can allow the user to enter non-numeric values in the field but use a validator to provide feedback to the user and disable the submit button.

提交回复
热议问题