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

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

Related

Hovering Dialogue Box

I am creating a log in form in Java. I have already completed the entire structure of the program, as well, I have designed it's purpose and perfected it's functionality. However, now I am focusing on styling the program. I'm proud of it as it is and it's fine if I don't add this feature, however I really would like to and cannot discover how.
In summary, when a username is typed into the login form, I have a void that runs after the JTextField loses focus. This void searches for possible invalid characters such as spaces. I can successfully change the color of the border on my JTextField, and other attributes I wish to change, however I also would like to have a small dialogue box to hover over the JTextField to say what specifically is wrong with what was typed. (e.g. "Your username cannot contain spaces!").
Ideally, it would be a rectangle that could simply be filled with text, and only appear when the username is deemed incorrect, and be able to disappear when it is fixed ( I can handle the appearance and disappearance most likely, I just need help with creating this box thing ).
Is there any such thing as like a "JHoverBox" or something that I could add to my JTextField?
Well, you can have, as Gene said, a label that's normally empty near the text field, but you can have it coloured like the background colour. This will make the user not able to see it, and when you detect something wrong, you can change the colour and add the text. Simple!

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)

Getting a Swing to read input without a submit button

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.

How can I create an AutoComplete popup in a JTextPane in Java?

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

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