当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Mismatching Javafx fx:id and variable name in controller class

问题描述:

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
分享给朋友:
您可能感兴趣的文章:
随机阅读: