问题
I desinged a little swing GUI that has some JTextFields, but it has a validateVariables method that has to validate all the fields that are inside the interface, there's one JTextField called (IP) must accept only int Variables how can i set it up like that?
P.S the JTextfield was created it in netbeans with the palete tool.
回答1:
This is the javadoc of JTextField http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html
There is an example
public class UpperCaseField extends JTextField {
public UpperCaseField(int cols) {
super(cols);
}
protected Document createDefaultModel() {
return new UpperCaseDocument();
}
static class UpperCaseDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null) {
return;
}
char[] upper = str.toCharArray();
for (int i = 0; i < upper.length; i++) {
upper[i] = Character.toUpperCase(upper[i]);
}
super.insertString(offs, new String(upper), a);
}
}
}
This example changes all user input to upper case. Just modify the insertString method, remove all non-digit characters, you can make your text field accept digits only.
Example:
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null) {
return;
}
super.insertString(offs, str.replaceAll("[^0-9]", ""), a);
}
---- EDIT ----
As @MadProgrammer said, DocumentFilter is another way to do so, for example:
Document document = someJTextField.getDocument();
if (document instanceof AbstractDocument) {
((AbstractDocument) doc).setDocumentFilter(new DocumentFilter() {
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String str, AttributeSet a) throws BadLocationException {
fb.insertString(offset, str.replaceAll("[^0-9]", ""), a);
}
});
}
回答2:
When I remember swing textfields correctly, you can register as an input/keylistener and validate the input on every keystroke.
回答3:
Make use of a DocumentFilter
, this is what it's designed for.
Take a look at Text Component Features, in particular Implementing a Document Filter and here for examples
回答4:
Just use a DocumentFilter
to accept only integers.
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;
public class InputInteger
{
private JTextField tField;
private JLabel label=new JLabel();
private MyDocumentFilter documentFilter;
private void displayGUI()
{
JFrame frame = new JFrame("Input Integer Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
tField = new JTextField(10);
((AbstractDocument)tField.getDocument()).setDocumentFilter(
new MyDocumentFilter());
contentPane.add(tField);
contentPane.add(label);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
new InputInteger().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class MyDocumentFilter extends DocumentFilter{
private static final long serialVersionUID = 1L;
@Override
public void insertString(FilterBypass fb, int off
, String str, AttributeSet attr)
throws BadLocationException
{
// remove non-digits
fb.insertString(off, str.replaceAll("\\D++", ""), attr);
}
@Override
public void replace(FilterBypass fb, int off
, int len, String str, AttributeSet attr)
throws BadLocationException
{
// remove non-digits
fb.replace(off, len, str.replaceAll("\\D++", ""), attr);
}
}
来源:https://stackoverflow.com/questions/21996430/input-text-only-accepts-numbers