在jTextfield中键入值后,是否有可能在不使用jButton的情况下自动按下? [英] Is it possible that after typing a value in jTextfield it will automatically press without using jButton?

查看:89
本文介绍了在jTextfield中键入值后,是否有可能在不使用jButton的情况下自动按下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在jTextfield中键入一个值后将自动按下而不使用jButton?我不知道它是否可能..扫描后是否会在我的条形码扫描仪中使用它并显示jTextfield会自动接收并在数据库上开始查询的值

Is it possible that after typing a value in jTextfield it will automatically press without using jButton? i dont know if its possible.. well ill be using it in my barcode scanner after the scan and display the values the jTextfield will automatically receive and start the query on database

推荐答案

答案取决于您对"press"的定义.根据我的经验,期望的是,在扫描条形码时,无需用户做任何额外的操作即可执行该操作

The answer depends on your definition of "press". In my experience, the expectations are, when the barcode is scanned, the action will be performed without the user having to do anything extra

基于此假设,您有两个基本选择

Based on that assumption, you have two basic choices

如果您知道条形码的长度(并且是恒定的),则可以使用DocumentFilter来检测何时达到该长度并触发操作

If you know the length of the bar code (and it's constant), you could use a DocumentFilter to detect when the length is reached and trigger a action

public class BarCodeLengthDocumentListener implements DocumentListener {

    private ActionListener actionListener;
    private int barCodeLength;

    public BarCodeLengthDocumentListener(int barCodeLength, ActionListener actionListener) {
        this.actionListener = actionListener;
        this.barCodeLength = barCodeLength;
    }

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

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

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

    protected void doCheck(DocumentEvent e) {
        Document doc = e.getDocument();
        if (doc.getLength() >= barCodeLength) {
            try {
                String text = doc.getText(0, doc.getLength());
                ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text);
                actionListener.actionPerformed(evt);
            } catch (BadLocationException exp) {
                exp.printStackTrace();
                ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null);
                actionListener.actionPerformed(evt);
            }
        }
    }

}

因此,基本上,这使您可以指定条形码的预期长度,当条形码到达时,它将触发ActionListener,使文本通过ActionEvent

So, basically, this allows you to specify the expected length of the bar code and when it's reached, it will trigger the ActionListener, passing the text through the ActionEvent

如果您不知道长度(或其变量),另一种选择是在文档事件发生与触发ActionListener

If you don't know the length (or it's variable), another option is to inject some kind of delay between when a document event occurs and when you trigger the ActionListener

public class DelayedDocumentListener implements DocumentListener {

    private ActionListener actionListener;
    private String text;
    private Timer timer;

    public DelayedDocumentListener(ActionListener actionListener) {
        this.actionListener = actionListener;
        timer = new Timer(1000, new ActionListener() {
                                        @Override
                                        public void actionPerformed(ActionEvent e) {
                                            ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text);
                                            actionListener.actionPerformed(evt);
                                        }
                                    });
        timer.setRepeats(false);
    }

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

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

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

    protected void doCheck(DocumentEvent e) {
        try {
            Document doc = e.getDocument();
            text = doc.getText(0, doc.getLength());
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
        timer.restart();
    }

}

因此,此操作使用Swing Timer,它在文档事件发生与触发ActionListener之间产生延迟(在这种情况下为1秒),每个新文档事件都会中断Timer,从而导致它重新启动.这意味着在上一个文档事件和ActionListener的触发之间必须至少有1秒.

So this uses a Swing Timer which generates a delay (in this case of 1 second) between when a document event occurs and when it will trigger the ActionListener, each new document event interrupts the Timer, causing it to restart. This means that there must be at least 1 second between the last document event and the triggering of the ActionListener.

由于有时人们需要手动输入条形码,因此您可能需要延迟播放.

Because sometimes people need to enter the barcode manually, you might want to play with that delay.

因此,这基本上代表了这两种想法,它使用java.awt.Robot将按键输入到键盘缓冲区中,该缓冲区应该模拟大多数条形码扫描仪

So, this basically presents both ideas, it uses a java.awt.Robot to inject key strokes into the keyboard buffer which should simulate most barcode scanners

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            BarCodeLengthDocumentListener lengthListener = new BarCodeLengthDocumentListener(7, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = e.getActionCommand();
                    JOptionPane.showMessageDialog(TestPane.this, text);
                }
            });
            DelayedDocumentListener delayedListener = new DelayedDocumentListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = e.getActionCommand();
                    JOptionPane.showMessageDialog(TestPane.this, text);
                }
            });

            JTextField field1 = new JTextField(7);
            field1.getDocument().addDocumentListener(lengthListener);
            JTextField field2 = new JTextField(7);
            field2.getDocument().addDocumentListener(delayedListener);

            add(field1);
            add(field2);

            JButton simLength = new JButton("Simulate Length");
            simLength.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    field1.setText(null);
                    field1.requestFocusInWindow();
                    Thread t = new Thread(new Simulator());
                    t.start();
                }
            });
            JButton simDelay = new JButton("Simulate Delay");
            simDelay.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    field2.setText(null);
                    field2.requestFocusInWindow();
                    Thread t = new Thread(new Simulator());
                    t.start();
                }
            });

            add(simLength);
            add(simDelay);
        }

    }

    public class Simulator implements Runnable {

        @Override
        public void run() {
            try {
                Robot bot = new Robot();
                type(KeyEvent.VK_1, bot);
                type(KeyEvent.VK_2, bot);
                type(KeyEvent.VK_3, bot);
                type(KeyEvent.VK_4, bot);
                type(KeyEvent.VK_5, bot);
                type(KeyEvent.VK_6, bot);
                type(KeyEvent.VK_7, bot);
            } catch (AWTException ex) {
                ex.printStackTrace();
            }
        }

        protected void type(int keyStoke, Robot bot) {
            bot.keyPress(keyStoke);
            bot.keyRelease(keyStoke);
        }

    }

    public class BarCodeLengthDocumentListener implements DocumentListener {

        private ActionListener actionListener;
        private int barCodeLength;

        public BarCodeLengthDocumentListener(int barCodeLength, ActionListener actionListener) {
            this.actionListener = actionListener;
            this.barCodeLength = barCodeLength;
        }

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

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

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

        protected void doCheck(DocumentEvent e) {
            Document doc = e.getDocument();
            if (doc.getLength() >= barCodeLength) {
                try {
                    String text = doc.getText(0, doc.getLength());
                    ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text);
                    actionListener.actionPerformed(evt);
                } catch (BadLocationException exp) {
                    exp.printStackTrace();
                    ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null);
                    actionListener.actionPerformed(evt);
                }
            }
        }

    }

    public class DelayedDocumentListener implements DocumentListener {

        private ActionListener actionListener;
        private String text;
        private Timer timer;

        public DelayedDocumentListener(ActionListener actionListener) {
            this.actionListener = actionListener;
            timer = new Timer(1000, new ActionListener() {
                                            @Override
                                            public void actionPerformed(ActionEvent e) {
                                                ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text);
                                                actionListener.actionPerformed(evt);
                                            }
                                        });
            timer.setRepeats(false);
        }

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

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

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

        protected void doCheck(DocumentEvent e) {
            try {
                Document doc = e.getDocument();
                text = doc.getText(0, doc.getLength());
            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }
            timer.restart();
        }

    }
}

这篇关于在jTextfield中键入值后,是否有可能在不使用jButton的情况下自动按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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