Vaadin Checking which component has focus - java

Is there a way to find out which component has the actual focus in a Vaadin window!?

No, at least not directly (which is surprising). Please follow the links and discussions indicated in the thread on Vaadin forum.
You can go around it, but it will require certain amount of work. A starting point is the Focusable interface. Basically, you should capture all focus events in all the components.

You can add a focusListener and a blurListener to each component. If a component has focus you set an object to the current component, if blurListener gets called you set it to null.
If the object is != null you just need to check which component it is and you know which one has focus.

Related

Remove/disable/override Swing's focus traversal keys

Note: see the edit (save some time reading)
I'm trying to make my mind-mapping program respond to shortcuts like CTRL+RIGHT (reordering nodes) and TAB (insert child at next indent level). I have a JPanel that handles all of the keystrokes. It resides inside of a JTabbedPane that might be the cause for Key Bindings not working. I've chickened out and decided to just use KeyListener.
The problem is that with the aforementioned key combinations, Swing automatically shifts the focus to some other component. I'd rather not manually put setFocusable(false) everywhere. How can I disable these shortcuts altogether in such a way that the focus will not be shifted, and the relevant KeyEvents will still be sent to my JPanel?
Edit:
I used the following code:
for (int id : new int[] {KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS})
setFocusTraversalKeys(id, Collections.EMPTY_SET);
to disable the default traversal keys (particularly TAB.)
Now the issue is actually why CTRL+UP causes a loss of focus. When pressing CTRL+DOWN, for instance, it's fine. The component behaves as expected. But with CTRL+UP, it works as expected and then focus is shifted/lost somehow. Can anyone say what CTRL+UP means and how to disable it wherever it is? Google isn't helping.
KeyBinding are used for all KeyEvents implemented in Swing APIs, maybe there is/are conflict
is required to override required KeyBindings, change used Keys, set to null, e.i. depends of your requirements
list of KeyBindings by #camickr

Listeners in forms with JComponents

Right now, when I have a form with many JComponents, mainly JTextFields, JTextAreas, JComboboxes, JCheckBoxes and JButtons and want to control their behaviour, for instance the change of focus after a certain key was released, I do the following:
I put all my components in a JComponent[] and cycle through it, adding the appropriate listener. When an event is registered by said listener, I check with "instanceof" what kind of JComponent fired the event and assign the proper reaction.
I use this method for instance to cycle with VK_ENTER through the form, or to "firePropertyChange(..)" after a DocumentListener fires, or to add UndoRedoListeners and so on.
My question : is there a better way to do this and if yes, can you explain to me the benefits ?
but my question refers to the general practice of putting all
JComponents in an array and cycling through them for every listener
and every fired event. It works fine enough, but it feels a bit
"uneconomic",so I wanted to know if it is recommended practice, or if
there is a better way of doing it.
I usually write a custom listener (often as an anonymous class) per type/ instance if I have type/ instance specific behavior so that I can avoid instanceof and other other checks.
You'll want to customise the focus tranfersal system.
Take a look at How to Use the Focus Subsystem, in particular Customizing Focus Traversal

How to refresh the lwuit form?

I am working in a project in which user fills a questionnaire and the answers are then submitted to the server. For simplicity I have just kept two questions per screen and after clicking on the next command the user gets next two questions. I have used lwuit framework in this project.
To reduce the memory requirements I create form, questLabel1, ansCombo1,questLabel2 and ansCombo2 only once. and their properties are set as per the current frame (screen). The problem is if you are in form 2 and you scroll down to the last option and then you click the next button, since you scrolled down the form doesn't displays the upper components even on the next form tried so many thing. creating a new instance of the form may work but I don't want to use that, for obvious memory reasons,
Any other solution?
thanks in advance,
To make it scroll so that component is visible, check Component/Container API javadocs. I've seen at lwuit page these suggest some methods with semantics that fits - scrollComponentToVisible, scrollRectToVisible. "Makes sure the component is visible in the scroll if this container is scrollable..." stuff like that
// above extracted from comment to an answer to make it more visible
// for the case if some reader has similar problem
Have you tried form.revalidate()?. Because this is useful when you modify the container hierarchy and need to redo the layout.
Update: Use requestFocus(); on first component of next form. Its automatically focused on first (Upper) component.
You can use form.refreshTheme() or form.revalidate() for refreshing your form. If you have made updates on any particular container then do the same for container as well.

Capturing "Tab" key press by both the focused component and its container

Is there a way to propagate a key press from say a JTextField to its container's KeyListener implementation?
So in effect, the keypress would be acted upon by both the text field and the JPanel. Right now the text field is consuming the key press so is nonexistent to the JPanel underneath.
In Swing, the tab key is used to change the focus from one component to another. The article Validating Input discusses InputVerifier, which may help you do what you want.
The question is why do you want to do this? What is your actual requirement as oppose to your attempted solution. Having an event handled by two components is generally not a good idea.
In general you should not use KeyListeners. Swing was designed to use Key Bindings. However, in this case it won't help because as mentioned earlier the focus subsystem handles the tab key.
If this is the only solution to your problem, then I think you can use KeyEventPostProcessor to listen for any KeyEvent. See Global Event Listeners for more info.

What's a HierarchyListener for?

Well, I guess it's for listening to hierarchy changes but I don't really understand what this means in practise.
Anyone has a good real life example when this should/could be used ?
Thanks
David
I used it once.
I was building a bubble-tip(a tooltip but with an arrow pointing to a target component) component. I used JLayeredPane with POPUP_LAYER to implement it.
So whenever the target component changed it's position or resized, I wanted a notification so that i could adjust my tip's arrow to point to it. This is my practical usage of HeirarchyListener. Now why could'y have I used ComponentListener ;)?
One example would be a component that should do something (e.g. display an animation) whenever it becomes visible. Its own visibility attribute is not sufficient, since visibility is inherited. A HierarchyListener allows it to be notified when the inherited visibility status changes.
Also:
http://www.google.com/search?q=%22implements+HierarchyListener%22

Categories