Controller of the father class
this class contains 3 button 'btnSocieta', 'btnUnitaLocali', 'btnReparti'. The first is enable from default other 2 are disable. In the stackPane i show the start page where you can find right away.
they have to change state after calling method setEnableBtnUnitaLocale or the other one
package View.Controllers;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Creazione implements Initializable {
@FXML
private JFXButton btnSocieta;
@FXML
private JFXButton btnUnitaLocali;
@FXML
private JFXButton btnReparti;
@FXML
private StackPane stackPane;
@Override
public void initialize(URL url, ResourceBundle rb) {
btnUnitaLocali.setDisable(true);
btnReparti.setDisable(true);
try {
Parent root = FXMLLoader.load(getClass().getResource("/View/fxml/creazione_societa.fxml"));
stackPane.getChildren().removeAll();
stackPane.getChildren().setAll(root);
} catch (IOException e) {
e.printStackTrace();
}
}
public void switchToSocieta(javafx.event.ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/View/fxml/creazione_societa.fxml"));
stackPane.getChildren().removeAll();
stackPane.getChildren().setAll(root);
}
public void switchToUnitaLocali(javafx.event.ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/View/fxml/creazione_unitalocale.fxml"));
stackPane.getChildren().removeAll();
stackPane.getChildren().setAll(root);
}
public void switchToReparti(javafx.event.ActionEvent event) {
System.out.println("You clicked me!");
}
public void setEnableBtnUnitaLocale() {
btnUnitaLocali.setDisable(false);
System.out.println("setEnableBtnUnitaLocale()");
}
public void setEnableBtnReparti() {
btnReparti.setDisable(false);
}
}
FXML file menaged by Creazione that have a StackPane that contains CreazioneSocieta
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<BorderPane xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="View.Controllers.Creazione">
<top>
<Pane prefHeight="48.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Label alignment="CENTER" layoutX="181.0" layoutY="16.0" minWidth="34.0" prefHeight="17.0" text=">>" />
<Label alignment="CENTER" layoutX="382.0" layoutY="16.0" text=">>" />
<JFXButton fx:id="btnSocieta" alignment="CENTER" layoutX="53.0" layoutY="12.0" onAction="#switchToSocieta" text="Società / Ente" />
<JFXButton fx:id="btnUnitaLocali" alignment="CENTER" layoutX="255.0" layoutY="12.0" onAction="#switchToUnitaLocali" text="Unità locali" />
<JFXButton fx:id="btnReparti" alignment="CENTER" layoutX="453.0" layoutY="12.0" onAction="#switchToReparti" text="Reparti" />
</children>
</Pane>
</top>
<center>
<StackPane fx:id="stackPane" prefHeight="352.0" prefWidth="600.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Controller of the FXML file
this controller only scope is to call the method 'setEnableBtnUnitaLocale' that we've see before through method 'salvaSocieta'
package View.Controllers;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
public class CreazioneSocieta implements Initializable {
@FXML
JFXButton btnSalva;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void salvaSocieta(javafx.event.ActionEvent event) throws IOException{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/fxml/main_creazione.fxml"));
Parent root = (Parent) loader.load();
Creazione controller = loader.getController();
controller.setEnableBtnUnitaLocale();
}
}
FXML File that contains the save button
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<Pane xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="View.Controllers.CreazioneSocieta">
<children>
<Label layoutX="51.0" layoutY="139.0" prefHeight="17.0" prefWidth="93.0"
text="Ragione Sociale:" />
<TextField layoutX="153.0" layoutY="135.0" prefHeight="25.0" prefWidth="437.0" />
<Label layoutX="51.0" layoutY="203.0" prefHeight="17.0" prefWidth="56.0" text="Indirizzo:" />
<TextField layoutX="115.0" layoutY="199.0" prefHeight="25.0" prefWidth="330.0" />
<Label layoutX="51.0" layoutY="324.0" prefHeight="17.0" prefWidth="56.0" text="Telefono:" />
<TextField accessibleText="INTEGER" layoutX="115.0" layoutY="320.0" prefHeight="25.0"
prefWidth="330.0" />
<Label layoutX="51.0" layoutY="262.0" prefHeight="17.0" prefWidth="43.0" text="e-mail:" />
<TextField layoutX="115.0" layoutY="258.0" prefHeight="25.0" prefWidth="330.0" />
<Label layoutX="467.0" layoutY="203.0" prefHeight="17.0" prefWidth="56.0" text="Provincia:" />
<TextField accessibleText="INTEGER" layoutX="533.0" layoutY="199.0" prefHeight="25.0"
prefWidth="56.0" />
<JFXButton onAction="#salvaSocieta" contentDisplay="CENTER" defaultButton="true"
graphicTextGap="0.0" layoutX="573.0" layoutY="421.0" prefHeight="45.0" prefWidth="0.0"
ripplerFill="#bfbdbd" textFill="WHITE" textOverrun="CLIP">
<graphic>
<ImageView fitHeight="37.0" fitWidth="37.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../icons/save_icon.png" />
</image>
</ImageView>
</graphic>
</JFXButton>
</children>
</Pane>
salvaSocieta(...)
method. You load themain_creazione.fxml
file, and call a method on its controller, but since you never actually do anything with the UI you loaded (i.e. you just put it in the variableroot
, and then do nothing) you obviously won't see the result of calling the controller method anywhere.Creazione
instance to theCreazioneSocieta
controller instance that is created when you loadcreazione_societa.fxml
. But really here you should use a MVC (or similar) design