JackY SmithS
JackY SmithS

Reputation: 3

Getting Started with JavaFX

Hello,when I study JavaFX in "Using FXML to Create a User Interface" https://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm

I want to write a demo like a simple calculator. when I use the GridPane layout, it can not have an error, but when I use the Pane layout, it showed the error "No Controller specified for top-level element" in the sample.fxml.

I do not understand it

I tried another way that I did not use FXML, wrote code in the Main.java like this

enter image description here

Upvotes: 0

Views: 308

Answers (1)

cementovoz
cementovoz

Reputation: 126

if you use action in fxml you will need to specify what class will be used to find this method, usually by attribute in root element like this

<GridPane fx:controller="sample.Controller"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">

more about https://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html#controller_method_event_handlers

or can use the method in FXMLoader when you have to use special instance

 FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
 loader.setController(new Controller());
 Parent root = loader.load();

Upvotes: 2

Related Questions