Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java FX fxml on Action

Tags:

I want to add a method to a button which is defined in my Controller class

in the console is only an error which tells me that it couldn't find the method

here is the code

sample.fxml

<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?>  <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Controller">     <children>         <Button layoutX="126" layoutY="90" text="lololol" onAction="#test"  fx:id="button" />     </children> </AnchorPane> 

and the Controller.java

package sample;  import javafx.fxml.FXML; import javafx.fxml.Initializable;  import java.awt.event.ActionEvent; import java.net.URL; import java.util.ResourceBundle;  public class Controller implements Initializable {     @Override     public void initialize(URL url, ResourceBundle resourceBundle)     {     }     @FXML     private void test(ActionEvent event)     {         System.out.println("lollolol");     } } 
like image 228
Jhon Smith Avatar asked Jun 27 '13 13:06

Jhon Smith


People also ask

How do I add FXML to another FXML?

The <fx:include> tag can be used to include one fxml file into another. The controller of the included fxml can be injected into the controller of the including file just as any other object created by the FXMLLoader . This is done by adding the fx:id attribute to the <fx:include> element.

What is FX ID in JavaFX?

In JavaFX id is used to set a CSS ID to a component. And fx:id is used for accessing that component in code (i.e. in a controller class). fx:id works like a components name.


1 Answers

Replace:

import java.awt.event.ActionEvent; 

with:

import javafx.event.ActionEvent; 
like image 109
Puce Avatar answered Oct 18 '22 13:10

Puce