• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to read from USB and display

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,

I'm trying to create a graphical program to read barcodes from a USB device. I have the following code:





When I get the barcode nothing is displayed and printed. When I click on the submit button, I see the barcode printed in the terminal.
What I would like is that every time I get a barcode, it is added and displayed in the table until I press submit. How can I do that?
 
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Late reply I know, I guess you've moved on by now (I missed this post or it was too complex to respond to when I saw it), but did you get any further with this?

First I would say, don't call stage.showAndWait(), just call show(). JavaDoc should help clear up why: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Stage.html#showAndWait

The double while(true) loops are not a great idea - I don't think you want the outer while loop at all. And your fileInputStream.close() method never gets called at the end, which is bad - after the return "Ok";, the method is left entirely.
For the inner loop you can do this instead, this is using something called a try-with-resources:



I would call the second class BarCodeReadingService (or at least something that's a noun instead of a verb type name) and refactor things so that all JavaFX stuff like TableView/TableColumn/Button/BorderPane/VBox etc. are in a method (displayCode() or something) in the "Main" class. Then that method would call the service class to read the barcode. The "service" class would focus ONLY on the barcode/USB concerns, to keep that decoupled from your UI code (that could be a conversation in itself but I'll just say it's considered a good practice, and that would make it independently testable without a UI being involved). The 'service' method would just have the stuff I pasted refactored above.

Last thing, you don't want to have potentially never ending loops or anything that might take a long time to execute run on the JavaFX thread, because that thread does rendering and updates for the UI itself. If it's locked in a never-ending loop or a very slow method, it never gets a chance to update the UI view, so it'll appear to "freeze".
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic