Agronaut022 Agronaut022 - 6 months ago 30
Java Question

JavaFX Cant use onAction

Hello I have an Strange Problem With JavaFX i am using the Controller The Scene Builder gives you.
The Problem Is that No Matter What a Menu on a Menu Bar does Nothing when Clicked.
This Is my Controller:

/**
* Sample Skeleton for 'Main.fxml' Controller Class
*/

package com.agronaut022.gui_changer.GUI.Scenes.Main;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.CodeSource;
import java.util.ResourceBundle;

import org.apache.commons.configuration.ConfigurationException;

import com.agronaut022.gui_changer.GUI.classes.MessageBox;
import com.agronaut022.gui_changer.etc.Config;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import junit.framework.Test;

public class MainController
`enter code here`{

@FXML
private ResourceBundle resources;

@FXML
private URL location;

@FXML
private ListView<?> ListView;

@FXML
private Button Change_Button;

@FXML
private Menu Help;

@FXML
private Menu about;

@FXML
private MenuBar MenuBar;

@FXML
private Pane pane;

@FXML
private ProgressBar ProgressBar;

@FXML
void initialize() {
assert Help != null : "fx:id=\"Help\" was not injected: check your FXML file 'Main.fxml'.";
assert ListView != null : "fx:id=\"ListView\" was not injected: check your FXML file 'Main.fxml'.";
assert Change_Button != null : "fx:id=\"Change_Button\" was not injected: check your FXML file 'Main.fxml'.";
assert about != null : "fx:id=\"about\" was not injected: check your FXML file 'Main.fxml'.";
assert MenuBar != null : "fx:id=\"MenuBar\" was not injected: check your FXML file 'Main.fxml'.";
assert pane != null : "fx:id=\"pane\" was not injected: check your FXML file 'Main.fxml'.";
assert ProgressBar != null : "fx:id=\"ProgressBar\" was not injected: check your FXML file 'Main.fxml'.";

Help.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("ABout");
Parent root;
try
{
root = FXMLLoader.load(getClass().getClassLoader().getResource("com/agronaut022/gui_changer/GUI/Scenes/About/About.fxml"),
resources);
Stage stage = new Stage();
stage.setTitle("About");
stage.setScene(new Scene(root, 600, 425));
stage.show();

} catch (IOException e)
{
e.printStackTrace();
}


}

});

/* Change_Button.setOnAction(new EventHandler<ActionEvent>()
{

@Override
public void handle(ActionEvent event)
{
Parent root;
try
{
root = FXMLLoader.load(getClass().getClassLoader().getResource("com/agronaut022/gui_changer/GUI/Scenes/About/About.fxml"),
resources);
Stage stage = new Stage();
stage.setTitle("About");
stage.setScene(new Scene(root, 600, 425));
stage.show();

} catch (IOException e)
{
e.printStackTrace();
}


}
}); */

}
}


And my FXML FIle is This

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.agronaut022.gui_changer.GUI.Scenes.Main.MainController">
<children>
<MenuBar id="MenuBar" fx:id="MenuBar" layoutY="2.0" prefHeight="25.0" prefWidth="350.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Import GUI" />
<MenuItem mnemonicParsing="false" text="Change Starmade Directory" />
<MenuItem mnemonicParsing="false" text="Create Details" />
</items></Menu>
<Menu mnemonicParsing="false" text="Backup" />
<Menu mnemonicParsing="false" text="Settings" />
<Menu fx:id="about" mnemonicParsing="false" text="About" />
<Menu fx:id="Help" mnemonicParsing="false" text="Help" />
</menus>
</MenuBar>
<ListView fx:id="ListView" layoutX="12.0" layoutY="43.0" prefHeight="450.0" prefWidth="326.0" />
<ProgressBar fx:id="ProgressBar" layoutX="12.0" layoutY="499.0" prefHeight="35.0" prefWidth="326.0" progress="0.0" />
<Button id="Change_Button" fx:id="Change_Button" layoutX="12.0" layoutY="540.0" mnemonicParsing="false" prefHeight="48.0" prefWidth="326.0" text="Button" />
</children>
</Pane>


I am not getting Any Errors And the Programm starts correctly,
I already Tried Using Diffrent Menu Items but the same results.

Answer

That's because a Menu without at least a Menuitem won't fire an event.

There has already been a question like yours. Here's a solution http://stackoverflow.com/a/19006643/5702956

Comments