0

What is the best / common practise to start running methods from a running JFrame according to user input?

Scenario The background is that I would like to have a JFrame running and a bar-code scanner attached. This bar-code scanner would scan not predefined (no fixed length) bar-codes (alpha numerical with special characters) and than look for special character like * and start an action.

The problem The format of the Bar-code is: * First Name * Surname * Birth-year. I was thinking of running my "analysing" method each time the contents of the JTextField changes but I believe that is necessary work there.

Lets say this is scanned * John * Doe * 1988 The characters from a bar-code scanner come in "one by one" and this would result in 13 unnecessary "content checks" .. And this is where I am stuck. I want my solution to be as efficient as is can :)

Note: Yes a solution could be letting the USER "confirm his input" after he scanned, by a button or some action and than "analyse the input" - but this is what I would like to avoid and make it fully automatic (and efficient)

How would your approach or tips and tricks be please?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Oliver Goossens
  • 1,903
  • 3
  • 20
  • 26
  • It's pretty easy for the user to hit enter when they are done typing in the text field. Add an action listener to the text field and it will fire when the user presses the enter key. – Andrew Thompson Jun 16 '16 at 07:23
  • Yeah :-/ I just wanted to avoid that - imaging the user scan one barcode after another without any unnecesasry delay whatsoever :) – Oliver Goossens Jun 16 '16 at 07:37
  • Who is putting the information in the field, software or a person? If software, how is the app. supposed to know when it is complete? Your question is still very unclear. – Andrew Thompson Jun 16 '16 at 07:39
  • Good point Andrew. I would say scanning takes approx 300ms. So maybe a timer would solve it. Like: change + 300 ms - start method. Hmm – Oliver Goossens Jun 16 '16 at 15:39

1 Answers1

2

SwingWorker is appropriate for this. Read the data in your implementation of doInBackground() and publish() the result when a complete record is available. Your implementation of process() can then safely update a view component's model on the event dispatch thread. As shown in this related example, you can cancel() and restart the worker as required. You may also need to reset your scanning state if there's been no input for some empirically determined time, e.g. 300 ms.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045