user3541602
user3541602

Reputation: 1

newbie toggle button event in javafx

<ToggleButton fx:id="toggleDC1" layoutX="52.0" layoutY="146.0" mnemonicParsing="false" onMouseClicked="#loadDC1" selected="false" text="click">
  <font>
    <Font name="Aparajita" size="22.0" fx:id="x1" />
  </font>
</ToggleButton>

I use JavaFX Scene Builder and i want catch event onMouseClicked or onMouseReleased, but i don't understand how to do. This is error when i click togglebutton:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1763)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3456)
at javafx.scene.Scene$ClickGenerator.access$8300(Scene.java:3387)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3755)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)

Can anyone give an example? Thanks in advance

Upvotes: 0

Views: 6789

Answers (2)

Aza Suhaza
Aza Suhaza

Reputation: 268

I face the same error as well & able to fix.

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch

Here is how your code suppose to look like

@FXML
public void loadDC1(ActionEvent event){
    ToggleButton btnSelected = (ToggleButton) event.getSource();

    String name=btnSelected.getId().toString();

    if(name.equalsIgnoreCase("toggleDC1")){
          //do something
    }
}

in JavaFX, you need to define all fx:id, #action in your fxml files into controller class like;

@FXML private ToggleButton toggleDC1; // as a class field

@FXML public void loadDC1(ActionEvent event){} // method

Upvotes: 1

brian
brian

Reputation: 10989

Toggle buttons are weird. They go in a ToggleGroup but they are added individually to a container.

final ToggleGroup tg = new ToggleGroup();
final ToggleButton tb1 = new ToggleButton("toggle 1");
tb1.setOnAction((ActionEvent e) -> {
    System.out.println("toggle1");
});
final ToggleButton tb2 = new ToggleButton("toggle 2");
tb2.setOnAction((ActionEvent e) -> {
    System.out.println("toggle2");
});
tg.getToggles().addAll(tb1,tb2);

hbox.getChildren().addAll(tb1,tb2);

I don't think you should use onMouseClicked for buttons. What about using the keyboard or touch?

Upvotes: 2

Related Questions