How to use TextChanged event? - java

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.

Related

Hitting enter to move a to next text field, like tab key?

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

Reading alt code JTextArea

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 ;)

GWT- PopupPanel with search?

I am looking for a way to create a popup dialog box when a user double clicks a textinput field that will contain a scroll-able list (from database table) where the user can select a field, hit ok, and have it placed into the textbox when popup closes.
The other major requirement is to have a filter/ or search field in the popup to aid the user in finding the correct option to select from quicker.
What is the best way to implement this?
Modification to gwt's popup panel? maybe a JOptionPane? are there any simple solutions already designed for free commercial use?
You could implement this with a com.google.gwt.user.client.ui.PopupPanel. You can make a PopupPanel that contains a ListBox with your data from the database, along with a OK button. When a user selects a value and hits OK, you should utilize an EventBus along with a custom Event that will pass the value to the field on the page. The page will have an event handler that will catch the event and put it into the field.
Another option is to use a com.google.gwt.user.client.ui.SuggestBox. It is a box that autocompletes / suggests values as you type, kind of like the Youtube search bar.
I can offer more resources to help you accomplish this, if you'd like.

How to bind properties of two UI objects in Java?

I have a simple Java application with textfields and buttons. I am looking for the best and quick way to bind the state of one JTextField to the state of one JButton. I'm using Eclipse, so I don't need any tricks of Netbeans IDE.
Suppose a user needs to input a value into a textfield in order to be able to send a request. The button should be enabled only if the value of the textfield is not empty and consists at least of 3 symbols. If the user deletes the input, the button becomes disabled.
I come from the Flex-world. Such a task can be solved there very easy. One should just write something like this:
<mx:Button enabled = "{myTextField.text.length >= 3}" />
Is there such an opportunity in Java? How is it called? I hope, I don't need to write event listeners for each pair of logicaly connected UI elements, do I?
I would do it with a DocumentListener on the JTextField. Every time the Document changes, you check the state of your button, like button.setEnabled(textField.getText().length > 3)

How do I fire an action when the user leaves a JTextBox?

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.

Categories

Resources