“No Controller specified for top level element” when programatically setting a Controller

后端 未结 2 1835
生来不讨喜
生来不讨喜 2021-01-04 07:27

I have an FXML file that has buttons with onMouseClicked attributes. I do not set a Controller up in the FXML because I have constructor injected data I want to provide to t

2条回答
  •  遥遥无期
    2021-01-04 08:26

    You need to add top level element fx:controller.

    Lets say you have a basic fxml file with a just anchor pane with a button like the fxml below.

    
    
    
    
    
    
    
    
       
          

    Your top level element will be the anchor pane in this case. If you want to use action buttons like onMouseClicked you need to tell to fxml you controller class in your top level element (in this case anchor pane) like below.

    
    
    
    
    
    
    
    
       
          

    fx:controller="com.example.Controller" line says that my control class is Controller which is in the com.example package.

    Also your elements id's should begin with fx like in the example (fx:id="buttonExample").

提交回复
热议问题