I got customized JComboBox which has the ability of autocomplete. It has attached KeyListener where I call HttpClient.send(request) and it returns some result array which I show in dropdown of combo box. For example, user starts typing "a" and the dropown offers all the results which starts with the letter a. The issue is I would like to wait like 2 seconds after the last character is written (so it won't send request after every key input), otherwise the app is laggy (response takes some time). I was thinking about using Runnable interface:
save time of last key press (using System.currentTimeMillis) into last variable
wait for 2 seconds
check if the value in last is still the same
if true, send request
The idea is if in those 2 seconds user types another character, the value of variable last is updated. Is this bad approach? Is it even doable? I got literally zero experience with multithreading. Can you give me an advice if I should do it this way or if there is some easier way?
I recommend to use kotlin coroutines ,
you can save your request in coroutine and cancel it when user type another letter
Related
I'm building a Discord bot in Java using JDA api. What my bot does is return a quote from a league of legends champion, and then the user must guess which champion says that by typing in the chat.
The problem I'm facing is that when a person calls the bot by the command on one server and also on another server or channel, the bot conflicts and the response changes to the last one that was generated, so the user who used the command first will not be able to get the answer right because it has changed.
To make it more clear, i will use a example:
Suppose I call a command that displays a math equation where the user must correctly answer it:
5 + 4
But someone else also uses the command on another server after i used the command:
2 + 3
The answer now is 5, because the last equation generated is the one that will counts.
What can i do to prevent this? To separate different processes to a single command?
I've tried to set a boolean to define if the command is active or not, but it happens the same.
Ex: someone used the command and a equation was generated, and then the boolean is set to true and then the same command can not be used. But this boolean is also preventing to someone else use the command in another server.
You could use a HashMap for storing the relevant data for each interaction. I'd like to suggest using the user id as key and storing question, correct answer and maybe the number of tries in an object.
Whenever someone uses your command you can check the map for their user id. If they are already in, you could discontinue their last attempt and start a new one or have them finish their last attempt before starting a new one.
Once a riddle is completed you just delete the respective element of the map.
Each time an user writes a message you just need to check if their id is a key in your map. If it is, you can react according to the entry, if not, just ignore the message.
I have a java swing application which has a form that I populate by scanning a barcode containing tab (or $I) delimited data as keyboard input through a USB connection. Intermittently, the form's text fields are incorrectly populated such that it appears the tab is processed too late. For example, if the data set in the barcode is something like 'abc$Idef', the expected output would be 'abc' in the 1st text field and 'def' in the 2nd text field. What we see sometimes instead is 'abcde' in the 1st text field and 'f' in the 2nd or even all data in the 1st text field and nothing in the 2nd.
I have seen this issue manifest at different frequencies across different days. Today could be good and I only see it happen 1 out of every 150 attempts. Yesterday it could have been poor, happening 1 out of 10 attempts. The scanner is at or near default factory settings with the exception of me toggling the parameter to implement tab vs $I delimiter. I have also attempted slowing down the transmission speed, and while that does appear to decrease the frequency of events, it does not eliminate them and the the slowed speed is not ideal for user workflow and so, reset it to full speed.
I am doubtful that the issue lies within the scanner however. in the application, I've attempted to disable all text field validations and data backups to essentially remove any custom code which might cause some delay, but the intermittent issue still exists. Currently the application is running on a WinXPSP3 using JRE 1.5.0_18. The scanner is a Symbol model ds6707. I could use some guidance in investigating this issue further to determine where the problem may lie.
Consider reading the stream on a separate thread and posting completed units on the EventQueue. This will ensure that events arrive "Sequentially…In the same order as they are enqueued." SwingWorker is convenient for this, as the process() method executes "asynchronously on the Event Dispatch Thread."
Hello guys i'm creating a web based label (barcode) printing application in Java (JSF) that requires printing via web.
A certain scenario was given during our testing:
If the user wants to print 10 copies of the same label (barcode), a field in the label which called the serial number must increment itself. Let say for example the last number generated was 100, then each label must have a unique serial number - 101, 102 ... 110.
I'm thinking of a way to call a function each time a printer prints a label or if there are any other ways to achieve this functionality.
Your help will be much appreciated and I'd like to thank you in advance for your response.
From your browser : propose a print button, which fill update (javascript) the page before opening the print dialog (JS : window.print). You can also have a look at onAfterPrint (http://www.webmasterworld.com/javascript/3038993.htm, IE only) to intercept in javascript the print action.
Other possible design (better) is to handle this on the server. This is the only way if you want that the concurrent users will not use the same serial numbers.
--> propose a button that gets a PDF generated by your server with the proper serial number. The pdf is then easily printed cleint side.
Of course, you will never be sure the pdf is actually printed. But having the printers theselves reporting directly to the server seems very complex to organize.
I have a textfield and if I want to write something to the field, it will show me the list of possible options regarding to that letter and I think this is called an auto complete.
Could someone give me an idea or a sample on how to do it?
Thanks..
Take a combo box and listen to all changes in the textfield. On every event, read the actual content and query your source list for possible matches. Then use the result to populate the associated list.
You may want to start autocompletion once the user has entered two or three letters, otherwise the list may get too long..
look here is AutoCompleteComboBox / JFextField, and there are two classes one for JComboBox, second for JTextField, notice auto-complete functionality requires both classes for that
I feeling generous as you really should google ...
As the user types, you'd need to query your DB with a like '<userInput>%' and return the results into a pulldown. You probably want to wait for a pause in the user's typing so as not to hammer your DB.
In the absence of a database, a data structure that would work well for this is called a Trie as you can traverse it past the initial input and present all the subsequent words.
So, I'm working with swing and I need to find a clean (non-CPU-hogging-way) to introduce a delay on a text field. Basically, users will enter a number into this field and the keyReleased() event checks that the input fits a few parameters and then assigns the value to a data storage element in the program. If the data is invalid, it displays a message. Since the routine is called every time they type a letter (unless they type VERY fast), the input process becomes quite annoying (as in general one or two characters of data are not going to fit the allowed parameters).
I've tried setting up a timer object and a timer task for it, however it doesn't seem to work very well (because it delays the thread the program is running on). The option to just wait until the data reaches a certain length is also not possible since (as state before) the input can vary in length.
Anyone got any ideas? Thanks!
I've done stuff like this fairly frequently, and I have two suggestions.
The standard way of dealing with this is to use the InputVerifier class. This however only operates when the input component loses focus - it's designed to prevent the user navigating out of an input field when it's invalid, but it doesn't check until then.
The other way I've done this is to check validity on every keystroke, but not to bring up a message when it's invalid. Instead use a color to indicate validity - e.g. color it red when its invalid and black when valid. This isn't nearly as intrusive as the message. You can use a tooltip to give more detailed feedback.
You can also combine these methods.
Write a custom DocumentFilter. Read the section from the Swing tutorial on Text Component Features for more information.