-1

Seems like i never gets correct reference from FXML.

FXML file:

<MenuBar xmlns:fx="http://javafx.com/fxml/1" fx:id="rootMenu" xmlns="http://javafx.com/javafx/8"
     fx:controller="my.MenuController">
<Menu mnemonicParsing="false" text="Analysis">
    <MenuItem fx:id="test" mnemonicParsing="false" onAction="#startNewAnalyze" text="New analysis"/>

Then in Controller file:

@FXML 
private static MenuItem test;

@FXML
private void initialize() {
    Systen.out.println(test.getText());
}
public static void setDisable(boolean enable) {
    test.setDisable(enable);
}

This will give Nullpointer when calling the MenuController.setDisable(false);

Any ideas?

Mikael
  • 633
  • 2
  • 8
  • 15
  • You can access them in the controller using the `fx:id`. I think you must be aware of it. – ItachiUchiha Nov 02 '15 at 13:48
  • You need to share the appropriate state between your different controllers using a model class (in this context, since the state is the state of the view, it would be considered a view model). See if http://stackoverflow.com/questions/32342864/applying-mvc-with-javafx helps. – James_D Nov 02 '15 at 14:41
  • Please read http://stackoverflow.com/help/how-to-ask There's not enough information here to really understand what you are doing. When do you call `setDisable(...)`, and from where? Where are the controls that are changed (in the Border Pane center, etc) defined and how and when do they change? What happens when you disable the menu item (do you get an error? nothing happens at all?)? Consider creating a [MCVE] and including it in your question. – James_D Nov 02 '15 at 15:24
  • Ok, what happens is that i never gets the reference of MenuItem, when running for example System.out.println(test.getText()) in the initialize() method i get error nullpointerexception directly when loading the FXML file as above. – Mikael Nov 02 '15 at 15:27
  • Problem is if i could miss something here in the fxml or controller ? – Mikael Nov 02 '15 at 15:28
  • Changed text in question! – Mikael Nov 02 '15 at 15:30
  • There is nothing wrong with the code you posted. If that is giving you a null pointer exception, there is something wrong somewhere else in your code. – James_D Nov 02 '15 at 15:42
  • Hi again, sure the code worked sorry, updated the question again! Could it be something with the static ? – Mikael Nov 02 '15 at 19:27
  • Yes. This question is a duplicate of http://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields/23109125#23109125 You cannot have `static` fields annotated `@FXML` (and it makes no sense at all to do so anyway). – James_D Nov 02 '15 at 20:40
  • Thanks James, work really good! Used getController() and shared the object between the classes! – Mikael Nov 03 '15 at 10:19

1 Answers1

0

You could make an MenuBar-Controller to access those items. Give your items and id with fx:id. In the Controller class you can use your variable easily by:

@FXML
private MenuItem 'yourID';
Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
  • See my answer below, still cant access them :( – Mikael Nov 02 '15 at 14:41
  • Do i need to initiate the private MenuItem test somehow? – Mikael Nov 02 '15 at 14:57
  • @Mikael: Don't initialize it. The `FXMLLoader` should do this. However in your fxml I don't see a xml element with the `fx:id="test"` attribute. Therefore none of the `MenuItem`s is assigned to your `private MenuItem test;` field. – fabian Nov 02 '15 at 15:13
  • Sorry meant MenuItem fx:id="change" still cant use it in the initialize () , any ideas hmmm – Mikael Nov 02 '15 at 15:20