Issue
I use IntelliJ IDEA 2021.3 and JavaFx17.
I am trying to change the screen that appears, but the program does not even open. Because of onAction button cannot resolve the handleButton1. I create the screens in SceneBuilder I added the controller class and I add the button function in the import button but it is not working.
My Controller Class
package com.example.family_tree;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import java.awt.event.ActionEvent;
import java.io.IOException;
public class Controller {
Stage stage;
Scene scene;
Parent root;
@FXML
//Button createButton, importButton;
public void handleButton1(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("import.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root, 750, 750);
stage.setScene(scene);
stage.show();
}
}
Main Class
public class Main extends Application {
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
stage.setScene(new Scene(root,750,750));
stage.setTitle("Family Tree Application");
// stage.initStyle(StageStyle.DECORATED);
stage.show();
stage.setResizable(true);
}
public static void main(String[] args) {
launch();
}
}
and the fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.ColorAdjust?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane focusTraversable="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="719.0" prefWidth="1217.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.family_tree.Controller">
<children>
<VBox layoutY="-7.0" prefHeight="719.0" prefWidth="310.0" style="-fx-background-color: #26a69a;">
<children>
<Pane prefHeight="234.0" prefWidth="309.0">
<children>
<Label alignment="TOP_LEFT" layoutX="48.0" layoutY="164.0" prefHeight="56.0" prefWidth="207.0" text="FAMILY TREE " textAlignment="CENTER" textOverrun="CLIP">
<font>
<Font name="Symbol" size="33.0" />
</font>
<effect>
<ColorAdjust brightness="-0.54" contrast="0.45" hue="-0.96" />
</effect>
<cursor>
<Cursor fx:constant="TEXT" />
</cursor>
</Label>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="87.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@family_tree.png" />
</image>
</ImageView>
</children>
</Pane>
<Button alignment="BASELINE_LEFT" graphicTextGap="10.0" mnemonicParsing="false" prefHeight="55.0" prefWidth="309.0" style="-fx-background-color: #0097A7;" text="CREATE FAMILY TREE">
<padding>
<Insets left="50.0" />
</padding>
<font>
<Font name="Arial" size="15.0" />
</font>
</Button>
<Button alignment="BASELINE_LEFT" graphicTextGap="10.0" mnemonicParsing="false" onAction="#handleButton1" prefHeight="56.0" prefWidth="309.0" style="-fx-background-color: #0097A7;" text="IMPORT FAMILY TREE">
<padding>
<Insets left="50.0" />
</padding>
<font>
<Font name="Arial" size="15.0" />
</font>
</Button>
<Button alignment="BASELINE_LEFT" graphicTextGap="10.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="309.0" style="-fx-background-color: #0097A7;" text="HELP">
<padding>
<Insets left="50.0" />
</padding>
<font>
<Font name="Arial" size="15.0" />
</font>
</Button>
<Button alignment="BASELINE_LEFT" graphicTextGap="10.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="309.0" style="-fx-background-color: #0097A7;" text="MADE BY" wrapText="true">
<padding>
<Insets left="50.0" />
</padding>
<font>
<Font name="Arial" size="15.0" />
</font>
</Button>
</children>
</VBox>
<Button layoutX="587.0" layoutY="208.0" mnemonicParsing="false" prefHeight="339.0" prefWidth="339.0" style="-fx-background-color: #4CAF50;">
<graphic>
<ImageView fitHeight="339.0" fitWidth="391.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Adsız.png" />
</image>
</ImageView>
</graphic>
</Button>
<Pane layoutX="309.0" layoutY="82.0" prefHeight="80.0" prefWidth="907.0">
<children>
<Label layoutY="-14.0" prefHeight="80.0" prefWidth="907.0" style="-fx-background-color: #26a69a;" text="CREATE NEW FAMILY TREE" textAlignment="CENTER">
<font>
<Font name="Times New Roman" size="31.0" />
</font>
</Label>
</children>
</Pane>
<VBox layoutX="983.0" layoutY="208.0" prefHeight="200.0" prefWidth="100.0" />
</children>
</AnchorPane>
Solution
Don't mix Swing/AWT code and JavaFX code by mistake (and usually not intentionally either).
Don't write:
import java.awt.event.ActionEvent;
Do write:
import javafx.event.ActionEvent;
Answered By - jewelsea
Answer Checked By - Robin (JavaFixing Admin)