I have a barcode that reads :
"SerialNumberALT09ProductNumber"
where ALT09 = Tab.
I read the number in to a JTextArea, if i write the number manually with the tab key on my keyboard i get a valid input i can then text.trim().split("\t");
to get a valid input to get respective serial number and product number. But when reading the barcode who sends ALT09 it does not read anything.
How do i get the JTextArea to accept ALT09 as Tab (or as alternative split on the location).
JTextArea is not getting Tab at all. Tab is interrupted by whole GUI interface to switch to next editable field. Of course you can bend this rule, intercept Tab on parent container and force it to send it to the child JTextArea and then you can even write your won method for KeyPressed event and insert Tab character into the text but it's good approach because it changes user experience. User expects Tab to go to next field but for this particular text area you say that it should be delimiter for your text? Another reason - Tab is similar for Space - so in user experience it's not clear was input right or not.
To avoid all these troubles why not take simple approach:
SerialNumber=ProductNumber
It is clear, visible, predictable, understandable and most off all - doesn't required your question ;)
Related
I am creating a simple form for a class and I want the user to be able to press enter after filling out one JTextField. It moves on to the next available JTextField (not all of them are editable at a given time), similar to using tab.
When it gets to the last editable JTextField it should submit the form. Is that possible or is tab and the usual enter function (as a submit my only option)?
I'll post the code if necessary, but if you can answer the question without it, that'd be great too.
Like Durandal mentioned, there are multiple ways to go about it.
You could use JTextField's addActionListener method, addAction method, addKeyListener method, etc... They're all fairly similar - you're telling the text field to do something when something happens. (Although exactly how you do it differs by method)
Regardless, each time the action will need to know which text field is next in line. Then you can call JTextField#requestFocus() to transfer input focus to it.
You can also do this with setFocusTraversalKeys and setNextFocusableComponent
I have a text box in which the user enters a unique number. After he enters a number and press tab or enter. I want to write query to search record matching to that unique number. The number is an integer. It will be passport no, pan card no or etc. If it is available in database, I want to display all information of that user.
I don't want to do that on button click.
I was looking for some TextChanged event but I found that the alternative in Java is the methods in document listener. But I am not getting how to use that. Also in Netbeans in the design view it does not show me any event on text changed. How to use Documentlistener to my JFrame/how to fix this?
Here you can find a tutorial on DocumentListeners and example code as well.
This is for an assignment so responses should not contain the code written for me.
I have written a program that is essentially an auto-complete program. It takes a word and returns the best matches.
I am trying to write a front end for it in swing(which I have no experience in) and want my front end to do the following: I want the input box to constantly be reading for user input, feeding that value to the other program, and returning the matches immediately in a drop down box, as, say, Google does. I can't seem to find any information on how to do this, all the intro tutorials use a submit button.
Can anyone explain to me how this would be done, or point me to a resource that could explain it? Again, please don't write the code for me, I don't want to unwittingly cheat on my assignment.
If you are using a JTextField, you could register a document listener on it.
If your input box is a JTextField, you can add a DocumentListener (this is a good tutorial) to capture character entries.
I think that no one from answerers ..., I'm only about Don't reinvent the wheel
1) use JTable with one (or two if is about Dictionary) Column and with basic implmentation for Sorting and Filtering (example with filtering from JTextField is in the Tutorial), JTable could be most complex from JComponents and there is everything (quite easilly) possible
2) use AutoComplete JComboBox / JTextField
3) use SwingX Decorator with JXList or JXTable
4) if you needed redirect output to the separate window then use JDialog / JWindow for popup window
One approach could be:
Attach a handler to detect a key press on the text box.
Grab the text from the box, and construct a "lookup" event which is runnable and submit this to some form of service which will dispatch it at some point in the future (hint: ExecutorService, Future)
Save this handle, and if the key press event happens again, cancel the previous and submit a new one.
When the event executes in the future and returns the result, popup a panel which displays the list of items.
I am creating a SQL editor. I am using JTextPane for the editor. I want to implement AutoCompletion for table name etc. like Eclipse.
I think the appropriate class for displaying info on top of another component is JPopupMenu, which already handles layering correctly to display itself. JPopupMenu has a show() method that takes its 'parent' component as an argument, and it will show itself in that component's coordinate space. Since you want to display a selection of terms for the user to choose from, a menu seems appropriate.
To check for text changes, you'd add a DocumentListener to the document that's wrapped by the JTextPane; you can access it using getDocument().
To find out where the cursor (actually, the caret) is, you can use getCaretPosition(). That returns the caret's position within the text stream as an int. You can use modelToView() to translate that position to actual (x,y) coordinates. That in turn will tell you where to show your menu.
You can use addKeyListener() to catch keyboard events on your JTextPane, like hitting Ctrl-Space.
The combination of all that should allow you to do what you're looking to do.
You can also use http://fifesoft.com/autocomplete/. You can install it on any JTextComponent.
For things like this you probably should consider layered panes so your auto-complete suggestions appear in the correct place and z-order.
Furthermore you will have to look for changes in the JTextPane to know when the user is typing and you will need a parser that understands what is typed so you can offer the feature only at appropriate points.
It's not quite clear what exactly your problem is and what you got so far.
I achieved this by adding a key listener to the JTextPane and checking for CTRL + Space keystrokes. When the appropriate key combo was detected the listener went off and looked up the list of possible matches based on the characters directly to the left of the cursor at the time of the key press and found the best matches and displayed them to the user in a JPopup. If there was an exact match then it simply replaced the partial text with the match. If no matches were found an option was given to the user to add the text that they had already typed, edit it and record it into the list of acceptable data.
We use jide. They have a lot of components that help you do this kind of thing really easily
I've got a JTextField, and I'd like the system to do some processing on what the user typed whenever the user leaves the text field. The ActionListener that you can add to just the JTextField only fires when the user presses enter, however.
I'd like the processing routine to run whenever the user leaves the text box by any means - tabs, clicks out of it, presses enter, etc. (The processing in question is to save the text the user typed to the appropriate data object, nothing fancy.)
My google-fu has failed on this one: I'm confident that it's possible, I just can't see how.
Add a FocusListener.
It's worth noting that this is a relatively low-level listener. On JComboBox it wont work unless you find the text field (and perhaps button) that the particular PL&F inserts. Swing is a bit odd that way (amongst many other ways).
Although for my money, non-cosmetic changes that happen when focus leaves a field give poor user experience. Much better to do any relevant changes on every change with a listener on the text field's document.
If you want to edit the text as it is typed then you should use an DocumentFilter.
If you want to validate the text as a complete entity then you can use an InputVerifier.