Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 完成对JTextField的条形码扫描后执行操作_Java_Swing_Timer_Jtextfield_Javax.swing.timer - Fatal编程技术网

Java 完成对JTextField的条形码扫描后执行操作

Java 完成对JTextField的条形码扫描后执行操作,java,swing,timer,jtextfield,javax.swing.timer,Java,Swing,Timer,Jtextfield,Javax.swing.timer,我有一个JTextFieldbarcodeTextField,它使用条形码扫描仪接收扫描条形码中的字符。据我所知,条形码扫描就像是非常快速地键入字符或在文本字段上复制粘贴字符barcodeTextField还用于显示建议,并用信息填充其他字段(就像在Google中搜索,在键入时会显示建议) 到目前为止,我使用DocumentListener实现了这一点: barcodeTextField.getDocument().addDocumentListener(new DocumentListener

我有一个
JTextField
barcodeTextField
,它使用条形码扫描仪接收扫描条形码中的字符。据我所知,条形码扫描就像是非常快速地键入字符或在文本字段上复制粘贴字符
barcodeTextField
还用于显示建议,并用信息填充其他字段(就像在Google中搜索,在键入时会显示建议)

到目前为止,我使用
DocumentListener
实现了这一点:

barcodeTextField.getDocument().addDocumentListener(new DocumentListener() {
  public void set() {
    System.out.println("Pass");
    // Do a lot of things here like DB CRUD operations.
  }

  @Override
  public void removeUpdate(DocumentEvent arg0) {
    set();
  }

  @Override
  public void insertUpdate(DocumentEvent arg0) {
    set();
  }

  @Override
  public void changedUpdate(DocumentEvent arg0) {
    set();
  }
});
问题是:如果扫描的条形码有13个字符,
set()
执行13次,DB操作也是如此。当我键入“123”以显示建议列表时,将执行3次
set()

我想在用户停止在
barcodeTextField
上键入时执行
set()
。在
Javascript/JQuery
中,当用户仍在键入时,可以使用
keyup()
事件并在
setTimeout()
方法中使用
cleartimout()
来完成此操作

如何在Java中为
JTextField
实现此行为

“当用户停止键入时,有没有办法获取在JTextField上输入的字符串?”


同样,Javascript有超时,Swing有计时器。因此,如果您正在寻找的是使用Javscript的“计时器”功能实现的,那么您可以看到是否可以使用

例如有一个。所以你可以在定时器上设置一个延迟,比如说1000毫秒。键入文本(文档中的第一次更改)后,检查,如果是,则执行
timer.restart()
,否则执行
timer.start()
(表示文档中的第一次更改)。只有在任何文档更改后经过一秒钟,计时器的操作才会发生。在第二个计时器启动之前发生的任何进一步更改都将导致计时器复位。并设置
timer.setRepeats(false)
使操作只发生一次

您的文档侦听器可能看起来像

class TimerDocumentListener implements DocumentListener {

    private Document doc;
    private Timer timer;

    public TimerDocumentListener() {
        timer = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (doc != null) {
                    try {
                        String text = doc.getText(0, doc.getLength());
                        statusLabel.setText(text);
                    } catch (BadLocationException ex) {
                        Logger.getLogger(TimerDemo.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        timer.setRepeats(false);
    }

    public void insertUpdate(DocumentEvent e) { set(e); }
    public void removeUpdate(DocumentEvent e) { set(e); }
    public void changedUpdate(DocumentEvent e) { set(e); }

    private void set(DocumentEvent e) {
        if (timer.isRunning()) {
            timer.restart();
        } else {
            this.doc = e.getDocument();
            timer.start();
        }
    }
}
下面是一个完整的示例,我通过以500毫秒的受控间隔显式插入文本字段的文档(九个数字)来“模拟”键入。您可以在DocumentListener拥有的计时器中看到延迟为1000毫秒。因此,只要输入发生,DocumentListener计时器就不会执行其操作,因为延迟超过500毫秒。对于文档中的每次更改,计时器都会重新启动

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class TimerDemo {

    private JTextField field;
    private JLabel statusLabel;

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            public void run() {
                new TimerDemo();
            }
        };
        SwingUtilities.invokeLater(runnable);
    }

    public TimerDemo() {
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(0, 1));

        field = new JTextField(20);
        field.getDocument().addDocumentListener(new TimerDocumentListener());
        statusLabel = new JLabel(" ");

        JButton start = new JButton("Start Fake Typing");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startInsertTimer();
            }
        });

        frame.add(field);
        frame.add(statusLabel);
        frame.add(start);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void startInsertTimer() {
        Timer timer = new Timer(500, new ActionListener() {
            private int count = 9;

            public void actionPerformed(ActionEvent e) {
                if (count == 0) {
                    ((Timer) e.getSource()).stop();
                } else {
                    Document doc = field.getDocument();
                    int length = doc.getLength();
                    try {
                        doc.insertString(length, Integer.toString(count), null);
                    } catch (BadLocationException ex) {
                        Logger.getLogger(TimerDemo.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    count--;
                }
            }
        });
        timer.start();
    }

    class TimerDocumentListener implements DocumentListener {

        private Document doc;
        private Timer timer;

        public TimerDocumentListener() {
            timer = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (doc != null) {
                        try {
                            String text = doc.getText(0, doc.getLength());
                            statusLabel.setText(text);
                        } catch (BadLocationException ex) {
                            Logger.getLogger(TimerDemo.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            });
            timer.setRepeats(false);
        }

        public void insertUpdate(DocumentEvent e) { set(e); }
        public void removeUpdate(DocumentEvent e) { set(e); }
        public void changedUpdate(DocumentEvent e) { set(e); }

        private void set(DocumentEvent e) {
            if (timer.isRunning()) {
                timer.restart();
            } else {
                this.doc = e.getDocument();
                timer.start();
            }
        }
    }
}


是的。其中一些使用了我不需要的库,因为我的问题不仅仅是条形码扫描,还包括用户类型的建议列表。当用户停止键入时,是否有方法获取在
JTextField
上输入的字符串?同样,Javascript有超时,Swing有计时器。因此,如果您所寻找的是在Javscript中使用其“timer”功能实现的,那么您可以查看是否可以让它使用一个例如
timer
具有
restart
。所以你可以在定时器上设置一个延迟,比如说1000毫秒。在第一次更换时启动计时器。接下来的一个变化是,检查
if(timer.isRunning())
,以及
timer.restart()
,否则
timer.start()
。只有在任何文档更改后经过一秒钟,计时器的操作才会发生。并设置
timer.setRepeats(false)
,这样操作只在计时器工作时发生。你可以这样回答。谢谢。你确实在很短的时间内完成了一个完整的摆动示例。:)谢谢