Pregunta

I'm building a basic Point of Sale application and I've been looking for ways of having my main POS JFrame listen for bar code input. I found this code (slightly modified) posted by Cyrusmith, which looks like what I want but I don't know how to implement it in my JFrame. It looks like its intended to be a separate class, which is how I have it in my project currently. I asked my coworker and he doesn't know either.

Thanks for your help.

package barcode;

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *   Listens for bar code input and puts it into a String Buffer.
 *
 */
public class BarcodeReader {

    private static final long THRESHOLD = 100;
    private static final int MIN_BARCODE_LENGTH = 8;

    public interface BarcodeListener {

        void onBarcodeRead(String barcode);
    }
    private final StringBuffer barcode = new StringBuffer();
    private final List<BarcodeListener> listeners = new CopyOnWriteArrayList<>();
    private long lastEventTimeStamp = 0L;

    public BarcodeReader() {

        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
            @Override
            public boolean dispatchKeyEvent(KeyEvent e) {
                try {
                    if (e.getID() != KeyEvent.KEY_RELEASED) {
                        return false;
                    }

                    if (e.getWhen() - lastEventTimeStamp > THRESHOLD) {
                        barcode.delete(0, barcode.length());
                    }

                    lastEventTimeStamp = e.getWhen();

                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        if (barcode.length() >= MIN_BARCODE_LENGTH) {
                            fireBarcode(barcode.toString());
                        }
                        barcode.delete(0, barcode.length());
                    } else {
                        barcode.append(e.getKeyChar());
                    }
                    return false;
                } catch (UnsupportedOperationException err) {
                    throw new UnsupportedOperationException(err); //To change body of generated methods, choose Tools | Templates.
                }

            }
        });

    }

    protected void fireBarcode(String barcode) {
        for (BarcodeListener listener : listeners) {
            listener.onBarcodeRead(barcode);
        }
    }

    public void addBarcodeListener(BarcodeListener listener) {
        listeners.add(listener);
    }

    public void removeBarcodeListener(BarcodeListener listener) {
        listeners.remove(listener);
    }
}
¿Fue útil?

Solución

Most bar code readers basically inject the codes directly into the keyboard buffer. So if you had a JTextField which had keyboard focus, the resulting text would be "entered" directly into it...no magic involved.

If you "want" to use this reader, then you will need to create an instance...

BarcodeReader reader = new BarcodeReader();

Register a BarcodeListener to it...

reader.addBarcodeListener(new BarcodeListener() {
    public void onBarcodeRead(String barcode) {
        // Respond to the event, like, I don't know,
        // set the text of text field :P
    }
});

But to me, this just seems like a lot of extra work - but that's just me...

So, yes, it's suppose to be a separate class. Depending on what you want to achieve, you could dump somewhere in your current code base, import the class into your source code and use it like any other. Equally, you could create a separate library for it, but this just means you need to include it within the classpath for compiling and runtime execution as well...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top