1

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

Filip Bartoš
  • 301
  • 3
  • 16
  • I linked the duplicate question. Unfortunately, IMO, there is not a really good solution to the duplicate question either. No good solution here :( It is, IMO, a failing of the toolkit that onAction handers cannot be defined directly on Menus, but only on MenuItems (and the latter cannot be placed into MenuBars). – jewelsea Dec 13 '17 at 23:53

1 Answers1

2

I think that Menu cannot be bind to onAction, but MenuItem CAN!

zlakad
  • 1,314
  • 1
  • 9
  • 16
  • 1
    https://ctrlv.cz/kvmR so why it is there ? and I cannot add menu item to menu bar. – Filip Bartoš Dec 13 '17 at 18:23
  • But you are right, when I add menu item and click on him it works! I will try to figure out solution thx – Filip Bartoš Dec 13 '17 at 18:26
  • 1
    I don't know why is there. But if you have Menu with no MenuItem(s), it is like the Button. – zlakad Dec 13 '17 at 18:29
  • I just need to create simple menu, this is what came to my mind do you do this any other way ? I need that Home menu has no submenus, just it to be pure button – Filip Bartoš Dec 13 '17 at 18:32
  • 1
    Try to put some Buttons in HBox, then combine them to MenuBar. It crossed my mind. But, maybe, you can access your Menu treating it like the Node - idk? – zlakad Dec 13 '17 at 18:36
  • Good idea, for now I will use it how its supposed to be used – Filip Bartoš Dec 13 '17 at 18:38
  • 2
    Unfortunately, you cannot access the menu by treating it like node because menu does not subclass node. There are skins for menus which might be linked to nodes but access to those is outside the scope of the public API, so anything relying on them would be unreliable. – jewelsea Dec 13 '17 at 23:57