Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

java - How to fixed keylistener on JTextField?

I have a Java swing application, so I have a simple Text box with KeyListener and I have a barcode reader (USB), when the barcode reader write the number on this textbox, I chek the code and I process it. But I have the problem the code can have from 4 to 13 digits. So I have this code

public class KeyListenerCodice implements KeyListener{
    public void keyPressed(KeyEvent click) {

    }

    public void keyReleased(KeyEvent keyEvent) {
        printIt("Released", keyEvent);
    }

    public void keyTyped(KeyEvent keyEvent) {
        printIt("Typed", keyEvent);
    }

    private void printIt(String title, KeyEvent keyEvent) {
        if(textCodice.getText().length()>=4 && textCodice.getText().length()<=13)
        {
            if(mappaArticoliScontrini.get(textCodice.getText().toUpperCase())!=null){
                inserisciProdotto();
            }
        }
    }
}

But sometimes the code have 13 digits but if the barcode reader is not very fast, the keylistener process only 4 or 5 or 6 digits and this is a problem.

How can I fixed it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The short answer is don't. Use a DocumentFilter to change what gets entered into a JTextComponent or a DocumentListener if you want to know when the content of the field changes.

KeyListener will not take into account what happens if the user pastes text into the field or if the field is modified programmatically

See DocumentFilter Examples and Implementing a Document Filter and Listening for Changes on a Document for more details

If your bar code scanner is injecting key events into the event queue, you may wish to inject an artificial delay into the DocumentFilter, as you won't want to process the field until AFTER all the key strokes are entered.

For example...

This basically uses a Swing Timer set to short delay (250 milliseconds in this case), each time the field is updated (and the DocumentListener is notified), it restarts the Timer. This means there must be delay of at least 250 milliseconds from the last update before the Timer can trigger the registered ActionListener and update the label.

public class TestPane extends JPanel {

    private Timer updateTimer;
    private JTextField field;
    private JLabel label;

    public TestPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        updateTimer = new Timer(250, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(field.getText());
            }
        });
        updateTimer.setRepeats(false);

        label = new JLabel("...");
        field = new JTextField(14);
        field.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                processUpdate();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                processUpdate();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                processUpdate();
            }

        });

        add(field, gbc);
        add(label, gbc);
    }

    protected void processUpdate() {
        updateTimer.restart();
    }

}

You might like to play around with the delay a little.

The bar code scanner may also be inserting a Enter key into the event queue, so it might be worth testing the field by registering an ActionListener against it


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...