JTextField validation - java

I wrote my own InputVerifier for JTextField and it works fine. Now I would like to "force" validation on this text field when user clicks a button. Is it possible to do?

Added as answer as #JarekMazur said it solved the problem.
Re-iterating my comment:
+1 #MadProgrammer for the docs
See this similar question/answer. It shows use how to programmatically trigger JTextField enter key pressed using Robot class and requestFocusInWindow(), though I think you would only need the latter.
Try changing JTextField focus using requestFocusInWindow().
You would initially request focus on the JTextField and than to another component in order to fire the Input Verifier shouldYieldFocus method.

Related

How to validate jTextfield within keypress

im trying to validate a jtextfield from the keypress and would like it to throw an error message in a jLabel. I am trying to do this with in the code below.
private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {
}
Can't comment, so i'll answer instead. If you are looking to validate the input of the user as he/she type, you might wanna have a look at javax.swing.text.DocumentFilter.
If you are simply looking to update a JLabel when the user types something, you should add a KeyboardListener to the JTextField. You have to be a bit careful with that tho, as swing doesn't guarantee the order in which listeners are called, meaning that the text may not have updated when the KeyboardListener is invoked.

Detecting JTextField "deselect" event

Is there some way to detect if a JTextField is deselected, i.e. the field WAS selected but now you have selected something else. The reason why I want to do this is because I want to check my users forms for any illegal characters before they try to submit their data. If there is some easier way to do that, instead of the way I'm trying to solve it, I'll gladly accept enlightenment.
At first though use a FocusAdapter and override focusLost(FocusEvent fe) which will be called when JTextField loses focuses, i.e another component is selected.
However because you have a purpose:
I want to check my users forms for any illegal characters before they
try to submit their data
Id suggest a read on How to Use the Focus Subsystem - Validating Input
Better options than a FocusAdapter - tailored made for validating/restricting user input:
DocumentFilter. See here and this variation for an example.
InputVerifier. See here for an example
JFormattedTextField. See here for an example

JGoodies Binding bug question about bufferedModels and keyboard input

I am using JGoodies Binding on a JTextField like so:
trigger = new Trigger();
PresentationModel<SpectralControlsModel> adapter = new PresentationModel<SpectralControlsModel>(model, trigger);
ValueModel valueModelStartingSampleJTextField = adapter.getBufferedModel("startingSample");
startingSampleJTextField = BasicComponentFactory.createLongField(valueModelStartingSampleJTextField);
setupValueModelListener(valueModelStartingSampleJTextField, startingSampleJTextField);
I have a keyListener on the JTextField that commits data to the model when the "enter" key is pushed.
The problem is when I enter in a new number in the JTextField and hit enter, the model gets the old value, even though I call trigger.triggerCommit(). If I enter a new number and then click something else, lose focus on the JTextField, and then gain focus again, then the new value ends up in the model as expected.
Let me know if I described the problem clearly enough, it is very strange behaviour and I need to get to the bottom of it, thanks.
UPDATE
I went to this Java2s.com JGoodies Example and added this code:
firstNameTextField.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
trigger.triggerCommit();
}
});
When I hit the enter button with degbugging on, the actionListener fires just like it would if I hit Commit Buffer Button, but nothing is committed when I display the values. I am very puzzled by this. There has to be a way to get JGoodies working with keyboard input. I shouldn't have to click out of the JTextField for the text to get to the Value Model.
I have a keyListener on the JTextField that commits data to the model when the "enter" key is pushed.
Probably not related to your problem, but you should be using an ActionListener to handle the Enter key, not a KeyListener.
Sorry about this question, I didn't explain it very clearly. Here is the answer though:
I looked through the JGoodies API (should have done this sooner) and found an unexpected static call, Bindings.commitImmediately()
If I call this method before my call to trigger.triggerCommit(), everything works as expected :)

Returning a JTextField to editing after a warning dialog

In my application I have a JTextField that will only allow the user to fill it with input formatted in a certain way. I have a method that checks if input into the field is valid and if not it displays a dialog. After this occurs I would like the focus to return to the text field so that the user can correct there mistake and type in the appropriately formatted input. I tried to do this by having the JTextField call requestFocus() on itself but this seems to only half work. The JTextField displays the flashing cursor but I cannot actually type in any text until I click on the text field again. Is there another method I need to call? I could not find anything else in the documentation.
It's not exactly the solution you asked for, but you might want to look at javax.swing.InputVerifier. This object allows you to verify the input to a field and prevent taking focus away from it if the format isn't right. I don't think it would allow you to put up a dialog explaining the error (though it might - try it) but it would certainly allow you to put a message in a 'message area'.
What you do is set an InputVerifier on the JComponent, have it test the contents for validity; if it isn't right then the JComponent won't release focus (and can write an error message).

How to trigger Java Swing InputVerifier on enter in JComboBox (actionPerformed)?

I have a Swing JComboBox with an InputVerifier set correctly.
I am using the combo box to set an integer.
If I type "cat" in the field and hit tab, my InputVerifier triggers and resets the value to "0".
If I type "cat" and hit enter, my InputVerifier is never called from actionPerformed. Do I need to explicitly call my InputVerifier from actionPerformed?
What's the best model to validate my JComboBox on tab and enter? It seems like this is something that should be given to me "for free" by the swing model.
The problem is "hit Tab" and "hit Enter" mean two different things in Java Swing. But those two actions mean the same thing to you, me, and the user.
Swing has no single mechanism to detect "when the user is done entering data". Instead, Swing focuses on the mechanics of "is this field losing keyboard focus" and "is the user pressing Enter key while inside a field".
Semantically those two actions mean the same thing from the user's perspective: "I'm done. Here's my input.". But, from what I can tell, Swing fails to offer a way to detect that user intention. I'm as surprised as you by the lack of such a feature, as this seems to be the most basic function of a form in a GUI. What we need, but don't have, is a "dataEntered" event.
There is a workaround…
In a similar context (JTextField instead of JComboBox) the Sun/Oracle Java Tutorial provides the example InputVerificationDemo where a class is created that:
Extends InputVerifier (to handle tabbing/clicking where focus is about to be lost)
Implements ActionListener (to handle pressing Enter key without leaving field)
The good thing about this workaround is that you can locate your handling code all in one place. The downside is that you still have the hassle of:
Creating a separate class.
Instantiating that class.
Passing that instance to both the setInputVerifier and addActionListener methods of your widget (JTextField, etc.).
This is the expected behavior of InputVerifier: the TAB key attempts to change focus, while the ENTER key does not. You can bind the ENTER key to a different action, as described in the tutorial How to Use Key Bindings. Also, consider the informative article Key Bindings, which includes a handy utility application.
When using an editable combo box, focus is on a JTextField which is used as the editor of the combo box. You can add an ActionListener to this text field.
In the ActionListener you could try invoking the transferFocus() method which should be equivalent to tabbing our of the text field. If that doesn't work then tha actionListener should invoke the same editing code as the InputVerifier.

Categories