I'm working with javafx and scene builder for the first time and from what I understand, the fx:id you create in scene builder for an object must match that object's variable name in the related controller class. For the GUI I'm writing right now, all the buttons follow that but when I add a ContextMenu, I get an error when the names match. HOWEVER, if I change the fx:id to NOT match the variable name in the controller class, the program will run. What is going on!?

EClassDTAT.fxml

<contextMenu>
   <ContextMenu fx:id="BasicShapesCST">
      <items>
         <MenuItem mnemonicParsing="false" text="Circle" fx:id="BSCircle" />
         <MenuItem mnemonicParsing="false" text="Square" fx:id="BSSquare" />
         <MenuItem mnemonicParsing="false" text="Triangle" fx:id="BSTriangle" />
      </items>
   </ContextMenu>
</contextMenu>

EClassDTATController.java

@FXML
private MenuItem BSCircle;
@FXML
private MenuItem BSSquare;
@FXML
private MenuItem BSTriangle;
@FXML
private ContextMenu BasicShapesCST;

Again to clarify, when the names match, I get an error, when the names mismatch, the program runs. Here is the error:

Can not set com.sun.webpane.platform.ContextMenu fieldapplication.EClassDTATController.BasicShapesCST to javafx.scene.control.ContextMenu /GUIworkspace/GUIs/bin/application/EClassDTAT.fxml:101
有帮助吗?

解决方案

Your import for ContextMenu is wrong. You have imported

com.sun.webpane.platform.ContextMenu 

where as you should have imported the context menu for javafx

javafx.scene.control.ContextMenu
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top