Domanda

I followed this example to only allow number input How to allow introducing only digits in jTextField?

but now it wont let me use textField.getText(); do I have to call a different method to get the text after using the document filter?

also does it need to be just a JTextField? is a formatted text field bad?

EDIT:

package inputinteger;

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());
    tField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            tFieldKeyPressed(evt);
        }
    });
    contentPane.add(tField); 
    contentPane.add(label);


    frame.setContentPane(contentPane);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}
private void tFieldKeyPressed(java.awt.event.KeyEvent evt) {
    label.setText(tField.getText());
}
public static void main(String[] args)
{
    Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {
            new InputInteger().displayGUI();
        }
    };
    EventQueue.invokeLater(runnable);
}
}

class MyDocumentFilter extends DocumentFilter{
    @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);
}
}

So this code works fine, but in the program im building, what im doing is using the NetBeans GUI builder and have a JFormattedTextField that I want to only accept Numbers as input. I tried adding Post creation code and it seems to have worked but now it throws an error that it cant find the JFormattedTextField (in my case creditInput)

It may just be something very simple that I am too tired to see right now. I will dig in deeper in the morning. If anyone can see my problem though id appreciate any input!

EDIT 2: This is the code I have in NetBeans

    javax.swing.JFormattedTextField creditsInput = new javax.swing.JFormattedTextField();
    ((AbstractDocument)creditsInput.getDocument()).setDocumentFilter(new MyDocumentFilter());

Later on in the code...

creditsInput.getText();//THIS LINE SAYS Symbol creditsInput cannot be found in class \

also I cant enter letters into the text box, so that part works i just need to be able to get the value of whats entered now.

È stato utile?

Soluzione

I fixed the problem. I dont know what I was doing wrong, but deleting the Formatted Text Field and using a regular TextField seems to work just fine.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top