Search code examples
javacssjavafxpdfhtml

Trying to setup an action handler for a button in JavaFX


I am trying to set up an action handler using Javafx but getting an error when compiling the code.

I am creating a website and I have decided to use JavaFX to run with my Java code because it supports CSS. The code posted is a simple test to see if everything works. So far, everything is working but when I set up the action handler I get an error. "Caused by: javafx.fxml.LoadException:". I would like "Hello World" to print on the console when the user presses the button.

CONTROLLER

import javafx.fxml.FXML;

public class Controller 
{                
    @FXML
    private void initialize() 
    {
    }

    @FXML
private void actionHandler() {
    System.out.println("Hello, World!");
    }}

MAIN

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
   Parent root = FXMLLoader.load(getClass().getResource("css_demo.fxml"));
    Scene scene = new Scene(root, 800, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
    scene.getStylesheets().add(getClass().getResource("demo.css").toExternalForm());
}

public static void main(String[] args) {
    launch(args);
}}

FXML FILE

<BorderPane >
    <top>
    <BorderPane styleClass="header-section" fx:controller="javafx.Controller" >

        <left>
            <Label id="header-text" text="Test Header"></Label>
        </left>
        <right>
            <Button id="btn"  text="Button" onAction="#actionHandler">
                <graphic>
                    <ImageView fitHeight="24" fitWidth="24" 
                        pickOnBounds="true" preserveRatio="true">
                        <image>
                            <Image url="@javafx_account_example.png" />
                        </image>
                    </ImageView>
                </graphic>
            </Button>
        </right>
    </BorderPane>
</top>
<left>
    <VBox styleClass="sidebar-section">
        <children>
            <Label text="Sidebar Item1"></Label>
        </children>
    </VBox>
</left>
<center>
    <VBox styleClass="content-section">
        <children>
            <Label id="content-header2" text="Header2"></Label>
            <Label id="header2">
                <text>
                     test
                </text>
            </Label>
        </children>
    </VBox>
</center>
</BorderPane>

CSS

.root {
-fx-font-size: 14px;
-fx-font-family: sans-serif;
-fx-background-color: #ffffff;}

.header-section {
-fx-padding: 10px;
-fx-font-size: 20px;
-fx-background-color: blue;}

.header-section Label {
-fx-text-fill: #ffffff;
-fx-padding: 10px;}

#btn {
-fx-background-color: transparent;
-fx-text-fill: #ffffff;
-fx-font-size: 16px;
-fx-cursor: hand;}

.sidebar-section {
-fx-min-width: 200px;
-fx-pref-width: 200px;
-fx-max-width: 200px;
-fx-border-width: 1;
-fx-border-color: transparent #E8E8E8 transparent transparent;}

.sidebar-section Label {
-fx-font-size: 18px;
-fx-padding: 10 15 10 15;
-fx-border-width: 1;
-fx-border-color: transparent transparent #E8E8E8 transparent;
-fx-min-width: 200px;
-fx-pref-width: 200px;
-fx-max-width: 200px;}

.content-section {
-fx-padding: 10 20 10 20;
-fx-wrap-text: true;}

#content-header {
-fx-font-size: 18px;
-fx-padding: 0 0 10 0;
-fx-font-weight: 700;}

#content {
-fx-wrap-text: true;
-fx-font-size: 16px;}

This is the error that I am getting when I compile the code.

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
/User/eclipse-workspace/demo/target/classes/demo/css_demo.fxml:15

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at demo.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[15,84]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributePrefixUnbound?BorderPane&fx:controller&fx
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
... 14 more
Exception running application demo.Main

Solution

  • There are 2 issues with the fxml:

    • The namespace declaration for the fx namespace is missing causing an error for the attribute fx:controller
    • You're only allowed to specify the controller at the root element. You specify the controller at it's top child though.
    • Processing instructions for imports are of course needed too, but I assume you left them out in the snippet you posted.

    The first part of the fxml snippet you posted should be changed to this:

    <BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafx.Controller">
        <top>
            <BorderPane styleClass="header-section">