JavaFX use a Button that is in a fragment of another FXML

0

This is the main screen driver, where the whole program should run, for example, when clicking on 'btMedicos' a small sub-menu with 3 Button opens inside 'menubar' and when clicking the Add button should open a form in the StackPane 'areaprincipal' but I can not map the 3 buttons that are on the other FXML page.

package com.vivabem.controller.javafx;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

/**
 *
 * @author willi
 */
public class MainController implements Initializable {

    @FXML protected StackPane areaprincipal, menubar;
    @FXML protected Button btMedicos;

    protected Parent menuMedico;
    protected Parent menuRecepcionista;
    protected Parent menuPaciente;

    protected Parent novoMedico;
    protected Parent editaMedico;
    protected Parent removeMedico;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initComponents();
    }

    public void initComponents() {
        try {
            FXMLLoader lMenuMedico = new FXMLLoader(getClass().getResource("/fxml/menumedico.fxml"));
            FXMLLoader lNovoMedico = new FXMLLoader(getClass().getResource("/fxml/novomedico.fxml"));

            menuMedico = lMenuMedico.load();
            novoMedico = lNovoMedico.load();

            lMenuMedico.setController(new menuMedicoController());
        } catch (IOException ex) {
            //tratar
        }
    }

    public void menuMedico(){
        if (menubar.getChildren().isEmpty()) {
            menubar.getChildren().setAll(menuMedico);
        } else {
            menubar.getChildren().clear();
        }
    }
}

This is the sub-menu's controller, Button 'btMedicoAdd' is what I'm trying to trigger on another page.

    public class menuMedicoController implements Initializable {

    @FXML protected Button btMedicoAdd;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        btMedicoAdd.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                novoMedico();
            }
        });
    }  

    public void novoMedico(){
        System.out.println("Clicou...");
    }

}

This is the main page FXML

    <AnchorPane maxHeight="768.0" maxWidth="1360.0" minHeight="400.0" minWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.vivabem.controller.javafx.MainController">
   <children>
      <VBox minHeight="800.0" minWidth="1200.0" prefHeight="768.0" prefWidth="1366.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <HBox style="-fx-background-color: #1abc9c;">
               <VBox.margin>
                  <Insets />
               </VBox.margin>
               <padding>
                  <Insets bottom="10.0" left="15.0" right="15.0" top="10.0" />
               </padding>
               <children>
                  <Button fx:id="btMedicos" contentDisplay="GRAPHIC_ONLY" mnemonicParsing="false" onAction="#menuMedico" prefHeight="50.0" prefWidth="50.0" style="-fx-background-color: #f39c12;" text="Medico">
                     <graphic>
                        <ImageView fitHeight="50.0" fitWidth="50.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../icons/medico.png" />
                           </image>
                        </ImageView>
                     </graphic>
                  </Button>
                  <Button contentDisplay="GRAPHIC_ONLY" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" style="-fx-background-color: #f39c12;" text="Recepcionista">
                     <graphic>
                        <ImageView fitHeight="50.0" fitWidth="50.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../icons/receptionist.png" />
                           </image>
                        </ImageView>
                     </graphic>
                     <HBox.margin>
                        <Insets left="10.0" />
                     </HBox.margin>
                  </Button>
                  <Button contentDisplay="GRAPHIC_ONLY" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" style="-fx-background-color: #f39c12;" text="Paciente">
                     <graphic>
                        <ImageView fitHeight="50.0" fitWidth="50.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../icons/Patient_Male_icon-icons.com_75053.png" />
                           </image>
                        </ImageView>
                     </graphic>
                     <HBox.margin>
                        <Insets left="10.0" />
                     </HBox.margin>
                  </Button>
               </children>
            </HBox>
            <HBox minHeight="0.0" minWidth="600.0">
                <StackPane fx:id="menubar" style="-fx-background-color: #1abc9c;" />
            </HBox>
            <StackPane fx:id="areaprincipal" minHeight="400.0" minWidth="600.0" />
         </children>
      </VBox>
   </children>
</AnchorPane>

And this is the FXML of the sub-menu

<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" style="-fx-background-color: #16a085;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" >
   <children>
      <Button fx:id="btMedicoAdd" contentDisplay="GRAPHIC_ONLY" mnemonicParsing="false">
         <graphic>
            <ImageView fitHeight="50.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../icons/add.png" />
               </image>
            </ImageView>
         </graphic>
         <HBox.margin>
            <Insets bottom="10.0" left="15.0" right="10.0" top="10.0" />
         </HBox.margin>
      </Button>
      <Button fx:id="btMedicoEdit" contentDisplay="GRAPHIC_ONLY" mnemonicParsing="false">
         <graphic>
            <ImageView fitHeight="50.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../icons/pencil.png" />
               </image>
            </ImageView>
         </graphic>
         <HBox.margin>
            <Insets bottom="10.0" right="10.0" top="10.0" />
         </HBox.margin>
      </Button>
      <Button fx:id="btMedicoRemove" contentDisplay="GRAPHIC_ONLY" mnemonicParsing="false">
         <graphic>
            <ImageView fitHeight="50.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../icons/user.png" />
               </image>
            </ImageView>
         </graphic>
         <HBox.margin>
            <Insets bottom="10.0" right="15.0" top="10.0" />
         </HBox.margin>
      </Button>
   </children>
</HBox>

If you have a easier way to do the same thing and someone can present you, I would appreciate it, I've been looking for a solution for two days and I have not found it.

    
asked by anonymous 11.08.2018 / 00:51

0 answers