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.
Related
I started making my own little applet with buttons and labels and text fields, but I want to when you enter a certain thing in the text field it does something. I tried if(fieldTextField.equals(mystringhere)) { but it doesn't work. Could you post an example of what I want?
You have to use a KeyListener, so that you can listen to keyboard and check your text everytime the user types something in your field:
KeyListener kl = new KeyAdapter(){
public void keyTyped(KeyEvent evt)){
if(yourTextField.getText().equals(yourString){
//do something here
}
}
};
yourTextField.addKeyListener(kl);
Note that yourTextField must be an instance variable or a local final variable in order to be used in the KeyListener methods
Take a look at event listeners (http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html). Also you shouldn't be comparing the TextField itself to the String, but it's text, using the getText() method.
EDIT: Better than the link above: http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html
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.
So as you may know, if you have a text field and you add an ActionListener to it, it will only listen to the keypress of the enter button. However, I want to let my ActionListener listen to changes in text of the . So basically I've got this:
public static JPanel mainPanel() {
JPanel mainp = new JPanel();
JTextArea areap = new JTextArea("Some text in the textarea");
JTextField fieldp = new JTextField("Edit this");
areap.setEditable(false);
fieldp.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(//change in textfield, for instance a letterpress or space bar)
{
//Do this
}
}
});
mainp.add(areap);
mainp.add(fieldp);
return mainp;
}
Any way I can listen to changes in text (like documented in the actionPerformed event)?
From an answer by #JRL
Use the underlying document:
myTextField.getDocument().addDocumentListener();
Yeah, but what is a document listener and how do you use it? You're not really answering the question.
I have a JTextField in my app's user interface. When the user makes any change to it, I want a nearby JCheckBox to be checked. The purpose is to tell the app to USE the value that was entered. Users often enter a value there but if they don't explicitly tell the app to use it then the app continues to ignore it. Instead of "training" users I'm supposed to follow the principle of least astonishment and automatically check the "Use this value" box.
But how do I listen for a change? Can't you guys just tell me the easy way, instead of "educating me" about document listeners?
Documents are the mechanisms java swing uses to store the text inside of a JTextField. DocumentListeners are objects that implement the DocumentListener interface and thus make it possible for you to list to changes in the document, i.e. changes in the text of the JTextField.
To use the document and documentlistener capabilities, as suggested above extend your class (probably but not necessarily a JFrame) so that it implements the DocumentListener interface. Implement all the methods for the interface (most likely your java ide can do that semi-automatically for you. FYI, the DocumentListener interface has three methods, one for inserting characters (into the text field), one for removing characters, and one for changing attributes. You are going to want to implement the first two as they are called when characters are added (the first one) or deleted (the second one). To get the changed text, you can either ask the document for the text, or more simply call myTextField.getText().
C'est tout!
Phil Troy
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 :)
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).