Reputation: 3850
DishFormPanelistIdLbl.setText("Panelist ID:");
DishFormPanelistIdTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DishFormPanelistIdTxtActionPerformed(evt);
}
});
I have here a text label and text field for ID i want to input only numbers in text how is it possible?been trying the sample program un net beans but they dont have such a solution for this problem any help is appreciated
"Update"
What i want here is when i type letter nothing will show but when number then it will show
Upvotes: 1
Views: 24501
Reputation: 3850
What i did is in the design mode i right click the text field > Events > Key > KeyTyped
then in the code i hade something like this
private void DishFormPanelistIdTxtKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
char enter = evt.getKeyChar();
if(!(Character.isDigit(enter))){
evt.consume();
}
}
i didnt get the solution here in stack but i will link it this is the link
Upvotes: 6
Reputation: 15146
You could do this by using a JFormattedTextField
. It's pretty straight forward: you pass it a formatter (which extends AbstractFormatter
), and the formatter will allow you to restrict and modify how things are displayed in your field. In this case, you could use the pre-made formatter NumberFormatter
:
NumberFormatter formatter = new NumberFormatter(); //create the formatter
formatter.setAllowsInvalid(false); //must specify that invalid chars are not allowed
JFormattedTextField field = new JFormattedTextField(formatter); //pass the formatter to the field
This will also add a comma where ever needed (for example, when you type in 1000, it'll display it as 1,000
.
Upvotes: 3