Kotlin FXML file controller

Wetys

I am trying to make basic desktop app using Kotlin and JavaFX(TornadoFX), but I am stuck on setting up controller for FXML file. I am following guide from edvin on git LINK to git.

The program should increment number in label whenewer is the button clicked.

The problem is.. When I try to use FXML file shown below, I can't use onAction="#increment" to "connect" that file with a controller. This will compile, but there is no way to call increment function from controller.kt file. Also there is error saying "No controller specified for top level element" ..

Whenever I try to specify the controller by using fx:controller=view.Controller the code will not even compile, showing error:

javafx.fxml.LoadException: Controller value already specified. 

Could someone please help me out?

Here is FXML file:

?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets top="20" right="20" bottom="20" left="20"/>
    </padding>

    <center>
        <VBox alignment="CENTER" spacing="10">
            <Label text="0">
                <font>
                    <Font size="20"/>
                </font>
            </Label>
            <Button text="Click to increment" onAction="#increment"/>
        </VBox>
    </center>
</BorderPane>

Here is MyApp.kt (main file):

package app

import javafx.stage.Stage
import tornadofx.*
import view.MainView

class MyApp: App(MainView::class){
    override val primaryView = MainView::class

    override fun start(stage: Stage) {
        stage.minHeight = 400.0
        stage.minWidth = 600.0
        super.start(stage)
    }

}

Here is MainView.kt (view):

package view

import javafx.scene.layout.BorderPane
import tornadofx.*

class MainView : View() {

    override val root: BorderPane by fxml("/views/MainViewFXML.fxml" )
}

Here is Controller.kt (this should be used to control FXML file actions):

package view

class Controller {

    fun increment(){
        //code
    }

}
Edvin Syse

In TornadoFX, the View IS the controller. Think of View subclasses as the View Controller. React to UI events in the View, and pass business logic off to a ViewModel or a Controller subclass.

Place your increment function in MainView and it will be called :) Remove the fx:controller=view.Controller attribute.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

For loop in FXML file || How to send data from controller to fxml file?

From Dev

For loop in FXML file || How to send data from controller to fxml file?

From Dev

ProgressBar doesn't work with a fxml file and a controller

From Dev

Null Point Exception when loading fxml file from controller of another fxml file

From Dev

JavaFX : How to dynamically change the controller in the same fxml file?

From Dev

Binding a Label's text property (in an FXML file) to an IntegerProperty (in a controller)

From Dev

How to add tags to an fxml file through controller in javafx?

From Dev

When I attach a controller to my fxml file, I get a constructLoadException

From Dev

JavaFX : How to dynamically change the controller in the same fxml file?

From Dev

JavaFX FXML controller ClassNotFoundException

From Dev

JavaFX FXML controller ClassNotFoundException

From Dev

How do I call an object an object from a .fxml file in my controller file?

From Dev

How to autowire Spring beans into a FXML controller class via a Spring XML config file

From Dev

How to change label text of .fxml file from another java class(means out side the controller)?

From Dev

Using JavaFX controller without FXML

From Dev

JavaFX - How to get FXML Controller?

From Dev

JavaFX FXML Controller onClose or equivalent?

From Dev

Constants in an FXML file

From Dev

Failed to load fxml file

From Dev

getHostServices().showDocument() in a FXML File

From Dev

Accessing fxml controller's method from fxml loader

From Java

JavaFX FXML controller - constructor vs initialize method

From Dev

JavaFX8 FXML Controller injection

From Dev

JavaFX Controller class variables not binding to their FXML counterparts

From Dev

No injectable field found in FXML Controller class

From Dev

Access elements of loaded FXML in Parent Controller

From Dev

How does FXMLLoader load the FXML's controller?

From Dev

Exception Raised when binding FXML and Controller

From Dev

javafx how controlfx dialog box in FXML controller

Related Related

  1. 1

    For loop in FXML file || How to send data from controller to fxml file?

  2. 2

    For loop in FXML file || How to send data from controller to fxml file?

  3. 3

    ProgressBar doesn't work with a fxml file and a controller

  4. 4

    Null Point Exception when loading fxml file from controller of another fxml file

  5. 5

    JavaFX : How to dynamically change the controller in the same fxml file?

  6. 6

    Binding a Label's text property (in an FXML file) to an IntegerProperty (in a controller)

  7. 7

    How to add tags to an fxml file through controller in javafx?

  8. 8

    When I attach a controller to my fxml file, I get a constructLoadException

  9. 9

    JavaFX : How to dynamically change the controller in the same fxml file?

  10. 10

    JavaFX FXML controller ClassNotFoundException

  11. 11

    JavaFX FXML controller ClassNotFoundException

  12. 12

    How do I call an object an object from a .fxml file in my controller file?

  13. 13

    How to autowire Spring beans into a FXML controller class via a Spring XML config file

  14. 14

    How to change label text of .fxml file from another java class(means out side the controller)?

  15. 15

    Using JavaFX controller without FXML

  16. 16

    JavaFX - How to get FXML Controller?

  17. 17

    JavaFX FXML Controller onClose or equivalent?

  18. 18

    Constants in an FXML file

  19. 19

    Failed to load fxml file

  20. 20

    getHostServices().showDocument() in a FXML File

  21. 21

    Accessing fxml controller's method from fxml loader

  22. 22

    JavaFX FXML controller - constructor vs initialize method

  23. 23

    JavaFX8 FXML Controller injection

  24. 24

    JavaFX Controller class variables not binding to their FXML counterparts

  25. 25

    No injectable field found in FXML Controller class

  26. 26

    Access elements of loaded FXML in Parent Controller

  27. 27

    How does FXMLLoader load the FXML's controller?

  28. 28

    Exception Raised when binding FXML and Controller

  29. 29

    javafx how controlfx dialog box in FXML controller

HotTag

Archive