JavaFX RequestFocus - java

I have an application, where the user can scan a barcode. Therefor I have a textfield, which gets the focus on first load. The application allows further actions for opening dialogs or triggering loading of data.
I need to set the focus to that field every time the user has finished this actions.
Is there any possibiltiy to capture all events on the main stage?
I have tried to add a listener to the focusProperty but this is just triggered on first load and on maximizing the window.

You can add a listener to the FocusedProperty. The following code:
btn.focusedProperty().addListener(new ChangeListener<Boolean>(){
#Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
//do something
}
});
works well in my case.

Related

Run some code when a popup closes in my swing app

In my app I display a popup using JPopupMenu. I want to run some code when this popup closes (either directly, programmatically or when escape key is pressed). For windows I can attach a WindowListener but JPopupMenu doesn't have any corresponding feature, and SwingUtilities.windowForComponent returns the root window of the app. How do I implement this?
How about adding a PopupMenuListener to it? Something like:
jpopMenu.addPopupMenuListener(new PopupMenuListener
{
public void popupMenuCanceled(PopupMenuEvent popupMenuEvent)
{
//here the code you want to be executed at close
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent){}
public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {}
}
This should be automatically executed when you cancel/close the popMenu. I didn't add code to the other two methods, but feel free to play with them if needed.

Why Handler doesn't work while Listener does his job?

I struggle to catch events after famous DOM.appendChild. After many attempts of use handlers I found this answer where #Ă•zbek did the job via listener.
And now I don't get it why "listener" works while "handler" not.
As an example in code:
Button button = new Button("Test button");
DOM.appendChild(getElement(), button.getElement());
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
GWT.log("this doesn't work");
}
});
DOM.sinkEvents(button.getElement(), Event.ONCLICK);
DOM.setEventListener(button.getElement(), new EventListener() {
public void onBrowserEvent(Event event) {
GWT.log("this works perfectly!");
}
});
the listener will be working but handler not.
So where is difference beetwen them?
And how to force handler to work?
Is there are some way to do this on handlers ?
I'm trying to understand the difference between listeners and handlers. I read these answers which shows that there is no big difference but I still don't get it
Button is a Widget not an element, and if you add it as an element to the dom you would lose the events. You can either append it to the RootPanel:
RootPanel.get().add(button) if you want to use the handler.
if you want to use the element not the widget you can use DOM.createButton(), and use the listener.
setEventListener of widgets is only called when they are "attached" (to a parent widget, which is itself attached, or delayed until it is attached), and you never "attach" the button.
With an explicit serEventListener you completely bypass the widget internals (and lifecycle). You could actually just use a ButtonElement in this case.
TL;DR: don't do that, it's a symptom of a broken design.

JavaFX equivalent to java.awt.EventQueue

Does JavaFX contain an equivalent to java.awt.EventQueue? For my application I need to intercept all GUI related events such as mouse and keyboard input, and so far I haven't been able to find a way to do it without attaching listeners to every GUI element.
This isn't quite the same as the question in your title, but to intercept all events, you can add an EventFilter to the Scene:
scene.addEventFilter(Event.ANY, new EventHandler<Event>() {
#Override
public void handle(Event event) {
System.out.println(event);
}
});
If you need to veto the event, call event.consume()

How does the GWT sinkEvent functionality works?

I have a simple GWT setup for testing:
<g:HTMLPanel ui:field="container" width="500px" height="300px">
<g:Label ui:field="inner" text="hello"></g:Label>
</g:HTMLPanel>
With handlers added:
container.addDomHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
Window.alert("click on container");
}
}, ClickEvent.getType());
inner.addDomHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
Window.alert("click on inner");
}
}, ClickEvent.getType());
Each of these addHandler calls, as we know from the sources,
DOM.sinkEvents(Element elem, int eventBits)
inside, and the latter, according to docs, "Sets the current set of events sunk by a given element. These events will be fired to the nearest {#link EventListener} specified on any of the element's parents."
But in fact, if you click on inner div the click will be fired to its parent div in any of the 3 ways:
if I have not set any ClickHandlers on child
if I have set handlers on child
if I have set the handlers, but cleared the sinking of ClickEvent with
DOM.sinkEvents(inner.getElement(), DOM.getEventsSunk(getElement()) & ~Event.getTypeInt(ClickEvent.getType().getName()));
Why this happens and what is the true role of sinking the events?
Why adding a Handler is related to sinking events, i.e. to firing the same event to parent of the given element?
Is it possible to prevent all handlers, added to the given widget from calling without removing them explicitely? I.e. is there a way to stop processing all events on some widget?
First, sinkEvent is about adding the native event handler on the element, as explained in the GWT wiki.
What you're seeing is event bubbling, which is how events work in browsers: there are 2 phases when dispatching an event: the capture phase (not implemented in old IEs) allows any ancestor element to catch the event before it reaches its target, then in the bubbling phase the event bubbles up the DOM hierarchy. So if you listen for clicks on the HTMLPanel, you'll also receive the bubbling click events from the Label.
You can prevent that by listening to the event at the Label level and calling stopPropagation() so it doesn't bubble up to the HTMLPanel.

AbstractAction as WindowListener

I'm trying to separate function from state in my GUI application by the use of Action objects. I've been successful in using these to create menu items and buttons that have the same functionality.
My problem is this: I want to have the same Action for both the 'exit' item in my menu and the close button of the frame.
Currently I've been able to solve it by adding the following WindowListener to the frame:
private class MainWindowListener extends WindowAdapter {
#Override
public void windowClosing(WindowEvent e) {
new ExitAction(model).actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Exit"));
}
}
Isn't there a simpler more straightforward way to do this?
Forwarding events is handy, but you can also use dispatchEvent(), as shown here.
Addendum: More examples that use Action are shown below.
LinePanel which connects buttons and keys.
ScrollAction which leverages existing Swing actions.
KeyPadPanel which illustrates forwarding actions.
GraphPanel which shows graph editor actions in a tool bar.

Categories

Resources