I have a frame with some JTextFields on it. When the user tabs out from a JTextField, I execute custom logic in the FocusTraversalPolicy, which sets some properties and based on them the FocusTraversalPolicy decides where should the focus go next. So far so good.
But I want this custom logic to execute everytime the JTextField loses focus(in an InputVerifier, because there is also validation in it).
Now the problem and the question: If the user leaves the field with the mouse the InputVerifier will be triggered and everything will be fine. But if the user presses TAB, both the FocusTraversalPolicy and the InputVerifier will be triggered and the custom logic will be executed twice, which I don't want. How can I detect in the InputVerifier that TAB was already pressed and don't execute anything? I want to execute the custom logic in the InputVerifier only when the field is left with the mouse. Or is there a better solution?
Hope my question is clear.
Regards, Petar
I ended up setting a boolean flag from the FocusTraversalPolicy if the custom logic is executed. Then in the InputVerifier if it is already executed I don't execute it again. Then I reset the flag.
Related
I have a popup component which has a first component JTextComponent and I need to recognize in FocusListener where from the focus gained to this component: when popup appears and the component get focus by default or user clicks on it. I didn't find the way to do it using FocusEvent.
Assuming a general use-case, what exactly is it that is tripping you? In my view:
Write the code you wish to execute when focus is gained in a method, say onFocusGained(boolean defaultFocus).
Call it from the FocusEvent methods with onFocusGained(true).
Call it from the MouseAdapter methods with onFocusGained(false).
I want to implement key listener on my puzzle game.
i have done it with action listener but now want to move on with key listener.
My logic for action listener was that:
when a specific button is clicked
it checks if is adjacent button's icon is null
if it is null then their icons will be swapped
Now, how can I do it with key listener?
Thank you.
if( b1==e.getSource()){
if(b2.getIcon()==null){
b2.setIcon(b1.getIcon());
b1.setIcon(null);
}
else if(b5.getIcon()==null){
b5.setIcon(b1.getIcon());
b1.setIcon(null);
}
}
You tell us that you have implemented a KeyListener but it's not working. Without code, all we can do is guess, so here's mine:
KeyListeners require focus to work, and so if your GUI has any components that steal the focus, such as JButtons, all JTextComponents such as JTextArea or JTextField, JComboBoxes, JLists,... then your KeyListener won't work.
One kludge is to force the component with the KeyListener to have the focus and to greedily hold on to the focus. This is not advisable since it will force your program to behave abnormally since most users expect buttons to have and retain focus when pressed, and text components won't work if they're not allowed focus.
Again, often the best solution is to use Key Bindings since these can work without requiring focus and are a cleaner way to capture keystrokes. Please look at the Key Bindings Tutorial and then have a look at my example code for using Key Bindings here and here.
Again for better and more specific help, then please tell us more of the details and show us your pertinent code, preferably as a minimal example program or MCVE.
I have a button called "save changes" that will save any changes if any changes are detected in a JTextField component. For now, I assume if the user types anything, then the content has changed.
I am using a KeyListener, but from this question it sounds like using anything other than an ActionListener is wrong?
You can add a DocumentListener to the document of the JTextField. ActionListener gets called only when the used presses enter. The advantage of using a document listener is that you can also detect changes made by other means than just by typing.
I'm working on a minesweeper in Java with Swing and I figured it'd be a fast way to get "rid" of a button that was clicked by using
JButton.setEnabled(false); (with a proper icon too, of course).
But do I have to remove all the listeners connected to this button later or is it enough and I can just forget about the said button then?
You have 2 different questions, one in your title, and one in your description.
Is removing actionListener necessary when you disable the button?
As stated in the previous comments, no.
But do I have to remove all the listeners connected to this button later...
Yes, if you have other kinds of listeners. For example, a MouseListener will still fire if the button is disabled. Usually, there is no need for a MouseListener on JButton, but there may be in some corner cases. I'm not sure about the other types of listeners that can be added to a JButton.
Just wanted to clarify.
I have JTextField with FocusListener that call textfield.grabFocus() every time when textfield lose focus. But unfortunetly, it allow parent Window to react on user action. Is there any way to don't give the focus to the Window using only JTextField methods?
If I'm understanding you correctly, you don't want any other controls on your program to be usable while this field requires focus. The problem is, each object is likely to have at least one event listener waiting for some action to occur, and stealing your focus away from you.
You can make all objects not focusable by setting setFocusable(false) for each object, but that will still allow events to be captured.
I'd override/replace (or possibly completely remove, if really necessary) the event listeners on all the other objects to only perform actions when the proper conditions are met (when your object doesn't require focus, if that would ever occur). If overridden/replaced, each listener could then return focus to the JTextField if those conditions are not met.
Also, it is better to use requestFocus() or requestFocusInWindow() instead of grabFocus(). See JComponent grabFocus() and JComponent requestFocus() for more information.