How to Make JTextField Accept Only Numbers
In this tutorial, we are going to see how to make JTextField accept only numbers. JTextField is a Swing component, so there must be a listener object for it to be useful. You can add KeyListener to prevent the user from entering non-numeric characters in a JTextField.
Example:
JTextField textField = new JTextField(15); textField.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if ( ((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) { e.consume(); // if it's not a number, ignore the event } } });
Here we ignore the captured event if it is a non-numeric character.
Complete Example : How to Make JTextField Accept Only Numbers
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class OnlyNumber extends JFrame { public static void main(String[]args){ new OnlyNumber().initComponent(); } public void initComponent() { setLayout(new FlowLayout()); JLabel lbl = new JLabel("Enter a number: "); JTextField textField = new JTextField(15); add(lbl); add(textField); textField.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if ( ((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) { e.consume(); // if it's not a number, ignore the event } } }); setSize(310,70); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } }
Output: