Saboor880 9 Junior Poster

Hi developers! I have made a POS software. I have added the functionality of "priniting to Thermal printer" and "barcodes". Firstly I added the functionality of Reciept printing which worked perfectly. Then I added the functionality of barcode scanning. As you know barcode reader device automatically scans the barcode and write the barcode in Text field or excel sheet. But in the sales screen , all the field of item/product should be populated from database When barcode is scanned. I implemented an interface(I get this code form internet) in the my Sales Screen Constructor. It workded perfectly for me, but after adding this interface, my Printing functionality malfunctioned.i-e after sending Print command to the Printer, Printer just waits and does not print any thing and the Printer status remains constant i-e"Printing".

But if I remove the Barcode scanning interface, The Printer works fine again, I want to know that why Implementation of Barcode scanning Interface, printer stops working? I think that the barcode scanning Interface (which i implemented) keeps a port busy , But I am not sure which port and why?

I am also giving the code of implementation of barcode scanning.

private AWTEventListener barcodeListener=null;

//now implementing the interface.
barcodeListener= (new BarcodeAwareAWTEventListener(new BarcodeCapturedListener() {
            @Override
            public void barcodeCaptured(String barcodeString) {
                   char[] strArray;
                 // JOptionPane.showMessageDialog(Sale.this, "barcode captured: "+barcodeString);
                 strArray = barcodeString.toCharArray(); 

        StringBuffer stringBuffer = new StringBuffer(); 

                 for (int i = 0; i < strArray.length; i++) {  
            if ((Integer.valueOf(strArray[i])!=32)) {  
                stringBuffer.append(strArray[i]);  
            }  
          }  

                String noSpaceStr = stringBuffer.toString();  

               char[] strArray2 = noSpaceStr.toCharArray(); 
              StringBuffer stringBuffer2 = new StringBuffer();
              for (int i = 0; i < strArray2.length; i++) {  
            if ((Integer.valueOf(strArray2[i])!=8)) {  
                stringBuffer2.append(strArray2[i]);  
            }  
          }  
              String noSpaceStr2 = stringBuffer2.toString();
             // JOptionPane.showMessageDialog(Sale.this, "barcode found: "+noSpaceStr2);

                try {
                    String itmName = obj.selectSingleItemByProductCode(noSpaceStr2);
                 //   JOptionPane.showMessageDialog(Sale.this, "product corresponding to code: "+itmName);
                    if(itmName.equals("")||itmName==null){
                       JOptionPane.showMessageDialog(Sale.this, "No Product found for this barcode.\n"
                               + "Try again to scan the barcode on Product.\n               OR\n check that whether you "
                               + "have entered the product\nwith this barcode in the inventory or not","Error",JOptionPane.ERROR_MESSAGE);
                    }
                    else{
                          // populating  the UI if barcode exists in database on successful barcode scann.
                       jComboBoxItem.setSelectedItem(itmName);
                     double tq = obj.SelectSumOfInventory(itmName);
                      txtfAvailQnt.setText(String.valueOf(tq));
                      sp= obj.getLatestSP(noSpaceStr2,itmName);
                      txtfSP.setText(String.valueOf(sp));

                       stringBuffer= new StringBuffer();
                       stringBuffer.append("");
                       stringBuffer2 = new StringBuffer();
                       stringBuffer2.append("");
                       itmName="";

                    }
                } catch (SQLException ex) {
                    Logger.getLogger(Sale.class.getName()).log(Level.SEVERE, null, ex);
                }

           }
        }));

          // Now adding the interface. 

       Toolkit.getDefaultToolkit().addAWTEventListener(barcodeListener, AWTEvent.KEY_EVENT_MASK);

Dani AI

Generated

Brief diagnosis and practical next steps for :

The behaviour you describe is far more commonly a threading/event-dispatch problem than a literal “port lock.” AWTEventListener callbacks run on the Swing Event Dispatch Thread (EDT). In the posted listener the barcode handler performs blocking work (database lookups and modal dialogs). Long or modal operations on the EDT will freeze normal event processing and can cause print jobs initiated from the UI to stall or never finish.

Quick tests to narrow it down

  • Temporarily register a listener that does nothing (or remove the listener) and verify printing. If printing works, the listener code is the culprit.
  • Comment out the DB calls and JOptionPanes inside the handler and test again.

Reliable fixes and design guidelines

  • Keep the AWT listener extremely small: collect the keystrokes and detect the end-of-barcode, then hand the complete string to a background thread for processing. Use a SwingWorker or an ExecutorService for the DB work, and update Swing components back on the EDT (SwingUtilities.invokeLater).
  • Never show a modal JOptionPane directly from the global key listener — queue the message to be shown after the background task completes.
  • Ensure the printing routine itself is not blocked on the EDT; if it uses callbacks that run on the EDT, avoid long-running work there.

Other checks

  • If the scanner is running as a keyboard wedge, it won’t hold a COM port. If it’s configured as a serial/USB-serial device, confirm it isn’t opening the same physical port the printer uses.
  • Remove the global listener on window close to avoid duplicate listeners. Add logging with timestamps around barcode events and print-job submissions to see the order of operations.

Following these steps will quickly show whether the issue is a blocked EDT (most likely) or an actual I/O conflict.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.