I am beginner to javafx, I am trying to make a menu and when someone click on menuitem it will print out text I have these codes
package sam;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author devby
*/
public class SaM extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("home.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
My controller looks like this
package sam;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
/**
*
* @author devby
*/
public class homeController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
//TODO
}
@FXML
public void callHome(ActionEvent event) {
System.out.println("Clicked on menu Home");
}
}
And my fxml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="401.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sam.homeController">
<children>
<MenuBar layoutY="2.0" prefHeight="26.0" prefWidth="600.0" AnchorPane.bottomAnchor="372.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="2.0">
<menus>
<Menu mnemonicParsing="false" onAction="#callHome" text="Home" />
<Menu mnemonicParsing="false" text="Library">
<items>
<MenuItem mnemonicParsing="false" text="Movies" />
<MenuItem mnemonicParsing="false" text="Serials" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Online" />
<Menu mnemonicParsing="false" text="Download" />
<Menu mnemonicParsing="false" text="Settings" />
</menus>
</MenuBar>
</children>
</AnchorPane>
When I run the program and click on Home menu button it does nothing. I am using javafx 8, IDE is netbeans and I am building GUI with Scene Builder Can anyone help ?
News: I have added a button and tried him. It works so probably something wrong with a menu