如何在JTextField上修复keylistener? [英] How to fixed keylistener on JTextField?

查看:139
本文介绍了如何在JTextField上修复keylistener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java swing应用程序,所以我有一个带有KeyListener的简单文本框,我有一个条形码阅读器(USB),当条形码阅读器在这个文本框上写下数字时,我会把代码交给我处理。但我有问题,代码可以有4到13位数。所以我有这段代码

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();
            }
        }
    }
}

但是有时代码有13位数但如果条形码阅读器不是很快,则keylistener只处理4或5或6位数,这是一个问题。

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.

我怎么能修好了吗?

推荐答案

简短的回答是没有。使用 DocumentFilter 更改输入 JTextComponent DocumentListener 如果你想知道该字段的内容何时发生变化。

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 将不会考虑如果用户将文本粘贴到字段中或者以编程方式修改字段会发生什么情况

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

请参阅 DocumentFilter示例实施文档过滤器收听有关更改的信息文档以获取更多详细信息

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

如果条形码扫描程序将关键事件注入事件队列,您可能希望在<$中注入人为延迟c $ c> DocumentFilter ,因为你不想要p在输入所有击键之后直到该字段为止。

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.

例如......

这主要用于每次更新字段时,Swing 计时器设置为短延迟(在这种情况下为250毫秒)(以及 DocumentListener 已通知),它重新启动计时器。这意味着在 Timer 可以触发已注册的 ActionListener 之前,必须延迟至少250毫秒。更新标签。

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.

条形码扫描器也可能在事件队列中插入 Enter 键,因此可能值得测试通过注册 ActionListener 对其进行字段

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

这篇关于如何在JTextField上修复keylistener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆