javafx.fxml.LoadException: Controller method "MyFunction" not found - event-handling

As described below, I Have an EventHandler linked to TextField (text variable), and I want to call a function from this handler that exists in the same class (MyFunction), so I'm getting the error (title of this post) :
Code :
class Example implements Initializable{
//...
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
//...
text.setOnKeyPressed(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event) {
if(event.getCode() == KeyCode.ENTER){
MyFunction(text.getText());
}
}
});
//...
}//function initialize ends here
public void MyFuction(String text){
//...
}
//...
}//Class ends here
I'm using javaFX 2.2, I hope my request is quite clear.

The problem seems to be in your FXML file. You are probably doing something like this: <Button text="Button" onAction="#MyFuction"/>while onAction handlers should accept single argument of a type that extends javafx.event.Event

Try to put #FXML in front of your MyFuction() method.

Related

Migration wicket 8.* from 7.*, problem with onRequestHandlerResolved() method on AbstractRequestCycleListener

I'm working on Wicket 8.* migration when I struggle on onRequestHandlerResolved() under IRequestCycleListener method conversion.
As per my structure of logic in my code wicket 7.15.0
public class MyRequestCycleListener extends AbstractRequestCycleListener{
//All methods override here
#Override
public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler) {
if (handler instanceof ListenerInterfaceRequestHandler) {
ListenerInterfaceRequestHandler requestHandler = (ListenerInterfaceRequestHandler) handler;
if (requestHandler.getListenerInterface().getListenerInterfaceClass().isAssignableFrom( IFormSubmitListener.class)) {
//here made some login as well as every onSubmit
}
}
}
}
As wicket 8.* changes
AbstractRequestCycleListener class converts to IRequestCycleListener interface
ListenerInterfaceRequestHandler renamed to ListenerRequestHandler
Deprecates RequestListenerInterface and also IFormSubmitListener makes error
How to convert code to migration on wicket 8.9.0 ?
It should be something like this:
public class MyRequestCycleListener implements IRequestCycleListener{
#Override
public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler) {
if (handler instanceof ListenerRequestHandler) {
ListenerRequestHandler requestHandler = (ListenerRequestHandler) handler;
if (requestHandler.getComponent() instanceof Form) {
//here made some login as well as every onSubmit
Form form = (Form) requestHandler.getComponent();
}
}
}
}

GWT / GWTBootstrap3 Extending Tooltip: Exception with addHandler in C'tor

I created the following class as an extension of gwtbootstrap3 Tooltip. There are at least 2 reasons why I want to derive the gwtbootstrap3 Tooltip class:
1.) Add a onWindowClosing Handler when the tooltip is shown so I can hide() the tooltip when the user leaves the page (this is - as far as I understand - a feature which is also not supported in Bootstrap, is it?)
2.) I want to prevent Tooltips from being shown when the page is displayed on iPads or iPhones as they behave strange when tooltips are involved (first tip shows the tooltip , the second tip executes the button, which is not exactly what the user expects)
Please note that the class given below is still not finished ... but already at this stage I get an exception when adding a handler.
Please also note that it throws an exception no matter what type of Handler (ShowHandler, ShownHandler, etc.) I add.
Any help greatly appreciated.
package com.mypackage.client.widgets.featureWidgets;
import org.gwtbootstrap3.client.shared.event.ShowEvent;
import org.gwtbootstrap3.client.shared.event.ShowHandler;
import org.gwtbootstrap3.client.ui.constants.Trigger;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ClosingEvent;
public class Tooltip extends org.gwtbootstrap3.client.ui.Tooltip {
private boolean isMobile;
private HandlerRegistration windowClosingHandlerRegistration;
private final Tooltip tooltip;
public Tooltip() {
super();
tooltip = this;
this.addShowHandler(new ShowHandler() {
#Override
public void onShow(final ShowEvent showEvent) {
// TODO Auto-generated method stub
if (windowClosingHandlerRegistration == null) {
windowClosingHandlerRegistration = Window.addWindowClosingHandler(new Window.ClosingHandler() {
#Override
public void onWindowClosing(final ClosingEvent arg0) {
tooltip.hide();
}
});
}
}
});
}
}
When I create a instance of this tooltip using the following:
[...]
<b:ButtonToolBar ui:field="itemButtonToolBar" addStyleNames="hiddenPrint">
<b:ButtonGroup>
<a:Tooltip title="{msgs.buttomTitleAddItem}" container="body">
<b:Button ui:field="addItemButton" icon="PLUS"/>
</a:Tooltip>
[...]
I get the following exception when trying to add the Handler, why?
SEVERE: (TypeError) : Cannot read property 'addHandler_11_g$' of undefinedcom.google.gwt.core.client.JavaScriptException: (TypeError) : Cannot read property 'addHandler_11_g$' of undefined
at Unknown.addShowHandler_2_g$(meetingApp-0.js#26:57195)
at Unknown.Tooltip_6_g$(meetingApp-0.js#8:57685)
at Unknown.build_f_Tooltip2_0_g$(meetingApp-0.js#55:31606)
at Unknown.get_f_Tooltip2_0_g$(meetingApp-0.js#15:31831)
at Unknown.build_f_ButtonGroup1_0_g$(meetingApp-0.js#38:31524)
at Unknown.get_f_ButtonGroup1_0_g$(meetingApp-0.js#15:31791)
at Unknown.build_itemButtonToolBar_0_g$(meetingApp-0.js#41:31696)
at Unknown.get_itemButtonToolBar_0_g$(meetingApp-0.js#15:31876)
at Unknown.createAndBindUi_58_g$(meetingApp-0.js#91:31437)
at Unknown.createAndBindUi_59_g$(meetingApp-0.js#15:31441)
at Unknown.ItemButtonGroup_2_g$(meetingApp-0.js#56:30733)
at Unknown.$init_589_g$(meetingApp-0.js#31:37722)
at Unknown.SummaryWidget_1_g$(meetingApp-0.js#8:37686)
at Unknown.loadSummaryWidget_0_g$(meetingApp-0.js#26:4991)
at Unknown.setSummary_1_g$(meetingApp-0.js#10:5028)
at Unknown.onSuccess_8_g$(meetingApp-0.js#21:3312)
at Unknown.onSuccess_9_g$(meetingApp-0.js#8:3317)
at Unknown.onResponseReceived_0_g$(meetingApp-0.js#26:156917)
at Unknown.fireOnResponseReceived_0_g$(meetingApp-0.js#17:129224)
at Unknown.onReadyStateChange_0_g$(meetingApp-0.js#28:129532)
at Unknown.<anonymous>(meetingApp-0.js#18:172082)
at Unknown.apply_0_g$(meetingApp-0.js#28:104636)
at Unknown.entry0_0_g$(meetingApp-0.js#16:104692)
at Unknown.<anonymous>(meetingApp-0.js#14:104672)
Disclaimer: I use gwtbootstrap3 v0.9.2 and I believe it's the same version as you use as I got the same error for your code.
A Tooltip needs a Widget to operate on (in your case the Button is a Tooltip's widget). Tooltip uses it's widget to do all events handling - see source code for addShowHandler for example.
Now you need to understand how the whole structure is built:
first the Tooltip is created (wit no widget set)
then the Button is created
Tooltip's setWidget method is called to set the button as a widget
So when you use addShowHandler method in your constructor, you actually call widget.addHandler while widget is null.
You can check it by Window.alert(tooltip.getWidget() == null ? "null" : tooltip.getWidget().toString());
There are few ways to make it work (the later the better):
wait for DOM structure to be built by scheduling a deferred command (if you are sure that the widget will be eventually set):
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
// set up events handling
}
});
override setWidget method (note thet there are two methods: setWidget(Widget w) and setWidget(IsWidget w)):
#Override
public void setWidget(Widget w) {
super.setWidget(w);
// set up events handling
}
you don't need to addWindowClosingHandler in the showEvent handler, you can do it directly in the constructor:
public class Tooltip extends org.gwtbootstrap3.client.ui.Tooltip {
private boolean isMobile;
private final Tooltip tooltip;
public Tooltip() {
super();
tooltip = this;
Window.addWindowClosingHandler(new Window.ClosingHandler() {
#Override
public void onWindowClosing(final ClosingEvent arg0) {
tooltip.hide();
}
});
}
}

SwipeRefreshLayout error

I am trying to implement swiperefreshlayout and I am getting error at "this"
public class viewBets_activity extends ActionBarActivity {
SwipeRefreshLayout swipeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewbets);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
}
I am getting error at swipeLayout.setOnRefreshListener(this); screenshot below
Well, now that you added the screenshot, the error is clear.
You're passing the wrong argument into setOnRefreshListener()! And of course, this makes sense, if you think about it. Your class is a ActionBarActivity. You're trying to set the OnRefreshListener as an ActionBarActivity...doesn't make any sense! You need to change your code to this:
swipeLayout.setOnRefreshListener(new OnRefreshListener()
{
#Override
public void onRefresh()
{
// what you want to happen onRefresh goes here
}
});
Here, you're creating a new OnRefreshListener object which you're adding as the listener.
For the future, in general, any time you have a setOn______Listener() method, the argument you'll be passing will be a On_____Listener object that you've customized. You can either created separately, or create it right in the set method the way I did in my answer.
Your class is missing
implement SwipeRefreshLayout.OnRefreshListener
This allows the listener to refer to the overridden method onRefresh when passing through this as the argument for setOnRefreshListener

Using HeaderResponseContainer: No FilteringHeaderResponse is present in the request cycle

I'm trying to add a custom HeaderResponseContainer in my wicket application. The tutorial looks quite simple (see Positioning of contributions), but when I add these lines and run the application I alwas get an IllegalStateException:
java.lang.IllegalStateException: No FilteringHeaderResponse is present in the request cycle. This may mean that you have not decorated the header response with a FilteringHeaderResponse. Simply calling the FilteringHeaderResponse constructor sets itself on the request cycle
at org.apache.wicket.markup.head.filter.FilteringHeaderResponse.get(FilteringHeaderResponse.java:165)
at org.apache.wicket.markup.head.filter.HeaderResponseContainer.onComponentTagBody(HeaderResponseContainer.java:64)
at org.apache.wicket.markup.html.panel.DefaultMarkupSourcingStrategy.onComponentTagBody(DefaultMarkupSourcingStrategy.java:71)
...
Yes, I already saw the note about FilteringHeaderResponse. But I am not sure where I should call the constructor. I already tried to add it in renderHead before calling response.render but I still get the same exception:
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
FilteringHeaderResponse resp = new FilteringHeaderResponse(response);
resp.render(new FilteredHeaderItem(..., "myKey"));
}
You can create a decorator that wraps responses in a FilteringHeaderResponse:
public final class FilteringHeaderResponseDecorator implements IHeaderResponseDecorator {
#Override
public IHeaderResponse decorate(IHeaderResponse response) {
return new FilteringHeaderResponse(response);
}
}
And that set it during application initialization:
Override
public void init() {
super.init();
setHeaderResponseDecorator(new FilteringHeaderResponseDecorator());
}
I just ran into this same problem and found that the Wicket In Action tutorial leaves out the part about setting up a custom IHeaderResponseDecorator in your main Wicket Application init. The Wicket guide has a more thorough example:
Apache Wicket User Guide - Put JavaScript inside page body
You need something like this in your wicket Application:
#Override
public void init()
{
setHeaderResponseDecorator(new JavaScriptToBucketResponseDecorator("myKey"));
}
/**
* Decorates an original IHeaderResponse and renders all javascript items
* (JavaScriptHeaderItem), to a specific container in the page.
*/
static class JavaScriptToBucketResponseDecorator implements IHeaderResponseDecorator
{
private String bucketName;
public JavaScriptToBucketResponseDecorator(String bucketName) {
this.bucketName = bucketName;
}
#Override
public IHeaderResponse decorate(IHeaderResponse response) {
return new JavaScriptFilteredIntoFooterHeaderResponse(response, bucketName);
}
}

GWT Widget.addHandler

I am trying to utilize Widget.addHandler(). However, the handler never gets called. Below is my sample code. What do I need to change to fix this?
My Handler Implementation:
public class CustomMouseMoveHandler
extends GwtEvent.Type<MouseMoveHandler>
implements MouseMoveHandler
{
#Override
public void onMouseMove(MouseMoveEvent event) {
System.out.println("----> onMouseMove.");
}
}
My EntryPoint.OnModuleLoad():
ContentPanel cp = new ContentPanel();
cp.setHeaderVisible(false);
cp.setHeight(com.google.gwt.user.client.Window.getClientHeight());
CustomMouseMoveHandler handler = new CustomMouseMoveHandler();
cp.addHandler(handler, handler);
RootPanel.get().add(cp);
/////
Added on 7/1/2011.
The following complete GWT simple code does not work either (with Jason's hint applied). Please help me out. Thanks
package tut.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class GwtHandler implements EntryPoint, MouseMoveHandler {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
TextArea comp = new TextArea();
comp.setSize("200px", "200px");
comp.setText("Testing Text");
comp.addHandler(this, MouseMoveEvent.getType());
RootPanel.get().add(comp);
}
#Override
public void onMouseMove(MouseMoveEvent event) {
com.google.gwt.user.client.Window.alert("onMouseMove");
}
}
GwtEvent.Type is used to dispatch events based on an event specific and unique object (object equality - == - is used to match event types). Passing your CustomMouseMoveHandler as the Type to addHandler indicates an event type other than that used for MouseMoveEvents (Indeed in this case every CustomMouseMoveHandler would be assigned to a different event Type since each object is different).
Instead of extending GwtEvent.Type<MouseMoveHandler> in your handler you need to get the event Type from MouseMoveEvent itself (using the static getType() method).
Don't extend GwtEvent.Type in your CustomMouseMoveHandler:
public class CustomMouseMoveHandler
implements MouseMoveHandler
{
...
}
And to add the handler:
cp.addDomHandler(handler, MouseMoveEvent.getType());
DomEvents have to be registered using addDomHandler, or you have to sinkEvents for their event type. addDomHandler is a shortcut for sinkEvents+addHandler.
Here's how I solved my problem. I wanted to add handlers to a NumberLabel. This is what worked:
final NumberLabel<Long> label = new NumberLabel<Long>();
label.setValue(2000l);
label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
MouseOverHandler handler = new MouseOverHandler() {
public void onMouseOver(MouseOverEvent event) {
System.out.println("mouse over");
}
};
Widget widget = label.asWidget();
widget.addDomHandler(handler, MouseOverEvent.getType());
Treating is as a Widget did the trick.
By the way, System.out.println worked.