How to Accept Only Numeric Values in a Jtextfield

Allow only numbers in JTextfield

Use DocumentFilter. Here is simple example with regex:

JTextField field = new JTextField(10);
((AbstractDocument)field.getDocument()).setDocumentFilter(new DocumentFilter(){
Pattern regEx = Pattern.compile("\\d*");

@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
Matcher matcher = regEx.matcher(text);
if(!matcher.matches()){
return;
}
super.replace(fb, offset, length, text, attrs);
}
});

field is your JTextField, and this filter allow to enter only digits.

Allow textfield to input only number [Java]

You need to create a subclass of DocumentFilter class and use a regular expression to match each inserted string/character if they are digits or not and perform actions accordingly.

Below is a fully working sample code of this working. Thanks to @camickr for pointing out using DocumentFilter is more up-to-date than the old way of extending JTextField to achieve the same result.

import java.awt.BorderLayout;
import java.util.regex.Pattern;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class TestDocumentFilter {

public static void main(String... args) {
new TestDocumentFilter();
}

public TestDocumentFilter() {
JTextField textField = new JTextField(10);
((AbstractDocument) textField.getDocument()).setDocumentFilter(new CustomDocumentFilter());

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(5, 5));
frame.getContentPane().add(textField, BorderLayout.NORTH);
frame.setSize(400, 200);
frame.setVisible(true);
}

private class CustomDocumentFilter extends DocumentFilter {

private Pattern regexCheck = Pattern.compile("[0-9]+");

@Override
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
if (str == null) {
return;
}

if (regexCheck.matcher(str).matches()) {
super.insertString(fb, offs, str, a);
}
}

@Override
public void replace(FilterBypass fb, int offset, int length, String str, AttributeSet attrs)
throws BadLocationException {
if (str == null) {
return;
}

if (regexCheck.matcher(str).matches()) {
fb.replace(offset, length, str, attrs);
}
}
}
}

JTextField accept only numbers and one dot

Don't use a KeyListener. That is old code when using AWT.

Swing has newer and better API's.

The easiest way is to use a JFormattedTextField. Read the section from the Swing tutorial on How to Use Formatted Text Fields for more information and working examples.

The other option is so use a DocumentFilter. Read the section from the Swing tutorial on Implementing a DocumentFilter.

How to accept only numeric values between 0 and 8 in JTextfied?

JFormattedTextField sounds like what you want.

NumberFormatter nf = new NumberFormatter();  
nf.setMinimum(new Integer(0));
nf.setMaximum(new Integer(8));
JFormattedTextField field = new JFormattedTextField(nf);

Edit: showed how to set min, max

Hibernate error - QuerySyntaxException: users is not mapped [from users]

In the HQL , you should use the java class name and property name of the mapped @Entity instead of the actual table name and column name , so the HQL should be :

List<User> result = session.createQuery("from User", User.class).getResultList();

Update : To be more precise , you should use the entity name configured in @Entity to refer to the "table" , which default to unqualified name of the mapped java class if you do not set it explicitly.

(P.S. It is @javax.persistence.Entity but not @org.hibernate.annotations.Entity)

QuerySyntaxException: User is not mapped when adding an entity from another package

Whelp, I found out what was going on. My galaxy brain decided to do addAnnotatedClass(anoClass.getClass()) on a Class object, so I was trying to declare java.lang.Class as a Hibernate Entity... Oh well, I'm just an idiot. Changed it to just addAnnotatedClass(anoClass) and it works just fine.



Related Topics



Leave a reply



Submit