Retrieve date from DatePicker and move to another FXML file - javafx

I got a question about passing a selected date from DatePicker to another FXML file. I’ve seen code in similar threads but I can’t seem to get my head around it.
Basically I want to select a date from DatePicker (in DiscountController) and be able to retrieve that selected date in another FXML file(in ConfirmController) and print it. Here is what I go so far.
Can anyone tell what I’m doing wrong/missing here?
public class DiscountController implements Initializable {
private final static DiscountController instance = new DiscountController();
public static DiscountController getInstance() {
return instance;
}
#FXML private DatePicker dp;
#FXML private Label lblDate;
public DatePicker getDp() {
return dp;
}
//button to print the selected date in a label. This works fine
#FXML private void ConfirmBtnAction (ActionEvent event) {
lblDate.setText(dp.getValue().toString());
}
//button to move to next FXML screen
#FXML private void nextBtnAction(ActionEvent event) {
try{
Parent parent = FXMLLoader.load(getClass().getResource("/appl/Confirm.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
catch(IOException e){
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
the fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ivikappl.DiscountController">
<children>
<Label contentDisplay="LEFT" layoutX="236.0" layoutY="33.0" prefHeight="42.0" prefWidth="133.0" text="Discount">
<font>
<Font size="28.0" />
</font>
</Label>
<Button fx:id="btnNext" layoutX="525.0" layoutY="358.0" mnemonicParsing="false" onAction="#nextBtnAction" prefWidth="61.0" text="Next" />
<Label layoutX="231.0" layoutY="158.0" prefHeight="21.0" prefWidth="69.0" text="Select Date" />
<Label layoutX="404.0" layoutY="158.0" prefHeight="21.0" prefWidth="101.0" text="Select discount %" />
<Label layoutX="54.0" layoutY="158.0" prefHeight="21.0" prefWidth="69.0" text="Product" />
<DatePicker fx:id="dp" layoutX="189.0" layoutY="117.0" />
<Label fx:id="lblDate" layoutX="219.0" layoutY="247.0" prefHeight="35.0" prefWidth="95.0" />
<Button fx:id="btnConfirm" layoutX="241.0" layoutY="200.0" mnemonicParsing="false" onAction="#ConfirmBtnAction" text="Confirm" />
<TextField layoutX="391.0" layoutY="117.0" />
<Label fx:id="lblProduct" layoutX="22.0" layoutY="121.0" prefHeight="17.0" prefWidth="149.0" />
</children>
</AnchorPane>
ConfirmController file
public class ConfirmController implements Initializable {
//button to fetch selected date from DiscountController and print it
//Having a problem, here, this one doesn't work
#FXML private void btnFetchAction (ActionEvent event) {
String A;
A = DiscountController.getInstance().getDp().getValue().toString();
System.out.println(A);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
and the FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ivikappl.ConfirmController">
<children>
<Button layoutX="382.0" layoutY="74.0" mnemonicParsing="false" prefHeight="49.0" prefWidth="115.0" text="Generate barcode" textAlignment="CENTER" />
<Label layoutX="91.0" layoutY="31.0" prefHeight="30.0" prefWidth="79.0" text="Summary">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Label layoutX="40.0" layoutY="123.0" prefHeight="20.0" prefWidth="85.0" text="Productname">
<font>
<Font name="System Italic" size="12.0" />
</font>
</Label>
<Label fx:id="lblDate" layoutX="40.0" layoutY="79.0" prefHeight="20.0" prefWidth="67.0">
<font>
<Font name="System Italic" size="12.0" />
</font>
</Label>
<Label layoutX="40.0" layoutY="166.0" prefHeight="20.0" prefWidth="67.0" text="Discount">
<font>
<Font name="System Italic" size="12.0" />
</font>
</Label>
<Button fx:id="btnFetch" layoutX="40.0" layoutY="236.0" mnemonicParsing="false" onAction="#btnFetchAction" text="Fetch" />
</children>
</AnchorPane>

You can't use the singleton pattern here because the FXMLLoader essentially creates an instance of DiscountController by calling the no-argument constructor (i.e. not by calling DiscountController.getInstance()). (You could go this route if you really wanted, by setting a controller factory on the FXMLLoader that loaded the discount FXML file, but it really doesn't make sense for controllers to be singletons. In many cases you really need multiple instances of them. And there are much simpler ways to achieve what you are trying to do anyway.)
Define a method in ConfirmController to which you can pass the date:
public class ConfirmController {
#FXML
private Label lblDate ;
public void setDate(LocalDate date) {
lblDate.setText(date.toString());
}
// ... other code as before...
}
and then just call it when you load the FXML:
public class DiscountController implements Initializable {
#FXML private DatePicker dp;
// ...
#FXML private void nextBtnAction(ActionEvent event) {
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/appl/Confirm.fxml"));
Parent parent = loader.load();
ConfirmController confirmController = loader.getController();
confirmController.setDate(dp.getValue());
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
catch(IOException e){
}
}
// ...
}
See Passing Parameters JavaFX FXML for a bunch of other ways to do this.

Related

Is there some form of priority or functionality issue when loading new FXML files into a Scene? (adopting framework provided by jewelsea)

I've realised only through clearing up code that I thought I could clear up and had cleared from some of my Controller classes where my issue is. To word my question better and provide more clarity to the concept of prioritisation I'll give a brief explanation followed by some code. Imagine an interface with a side of the screen being consistent and permanent to the interface where you have Buttons for menu-ing where you essentially move between tabs changing the rest of the Stage passing fxml files to the Pane associated.
Reading examples and questions I understood this to be viable and using the framework from this link https://gist.github.com/jewelsea/6460130 seeing a header remain consistent while switching Scene between vista1.fxml and vista2.fxml, my understanding was that if the Header remains from main.fxml as part of the scene that the Stage is therefore using either both main.fxml and vista1.fxml or main.fxml and vista2.fxml concurrently and thus both associated controllers exist and will have access to corresponding methods according to the Pane interacted with.
That is clearly not the case though and the consistent Pane in this case main.fxml does not have its controller since when the other is loaded and creates its controller it replaces or clears the other controller in some form or another I now presume is the case. I thought I could avoid code duplication by using separate Panes with the consistency of the menu bar but also being able to use its buttons.
Some code as an example:
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception{
stage.setTitle("Vista Viewer");
stage.setScene(createScene(loadMainPane()));
stage.show();
}
private Pane loadMainPane() throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(
VistaNavigator.MAIN));
MainController mainController = loader.getController();
VistaNavigator.setMainController(mainController);
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
return scene;
}
public static void main(String[] args) {
launch(args);
}
}
VistaNavigator.java
package sample;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
public class VistaNavigator {
public static final String MAIN = "main.fxml";
public static final String VISTA_1 = "vista1.fxml";
public static final String VISTA_2 = "vista2.fxml";
public static final String OTHER_MENU_OPTION = "otherMenu.fxml";
private static MainController mainController;
public static void setMainController(MainController mainController) {
VistaNavigator.mainController = mainController;
}
public static void loadVista(String fxml) {
try {
mainController.setVista(
FXMLLoader.load(
VistaNavigator.class.getResource(
fxml
)
)
);
} catch (IOException e) {
e.printStackTrace();
}
}
}
MainController.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
public class MainController {
#FXML
private StackPane vistaHolder;
public void setVista(Node node) {
vistaHolder.getChildren().setAll(node);
}
public void homebtn() throws Exception{
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
public void otherMenuOption() throws Exception{
VistaNavigator.loadVista(VistaNavigator.OTHER_MENU_OPTION);
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.MainController">
<children>
<VBox prefWidth="155.0">
<children>
<Label fx:id="headerLabel" maxWidth="120.0" text="Header" />
</children>
</VBox>
<Pane layoutY="40.0" prefHeight="200.0" prefWidth="120.0" style="-fx-background-color: #091D34;">
<children>
<Button fx:id="button1" layoutY="60.0" mnemonicParsing="false" onAction="#homebtn" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="Home" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button fx:id="button2" layoutY="110.0" mnemonicParsing="false" onAction="#otherMenuOption" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="OtherMenuOption" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
</children>
</Pane>
<StackPane fx:id="vistaHolder" VBox.vgrow="ALWAYS" />
</children>
</AnchorPane>
otherMenu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="otherMenuOption" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller">
<children>
<Label fx:id="label" layoutX="135.0" layoutY="57.0" maxWidth="120.0" text="other program stuff" VBox.vgrow="NEVER" />
</children>
</StackPane>
Vista1Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class Vista1Controller {
#FXML
void nextPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_2);
}
}
vista1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller" prefHeight="400.0" prefWidth="600.0">
<children>
<Pane fx:id="Vista1" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
<Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#nextPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista2" textFill="#ffffff">
<font>
<Font name="Britannic Bold" size="20.0" />
</font>
</Button>
</Pane>
</children>
</AnchorPane>
Vista2Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class Vista2Controller {
#FXML
void previousPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
}
vista2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista2Controller" prefHeight="400.0" prefWidth="600.0">
<children>
<Pane fx:id="Vista2" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
<Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#previousPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista1" textFill="#ffffff">
<font>
<Font name="Britannic Bold" size="20.0" />
</font>
</Button>
</Pane>
</children>
</AnchorPane>
Is there a good way to maintain this design and prevent code duplication within the specific controllers and would it be through inheritance or passing parameters, I'm not sure what this is comparable to nor whether it's possible?

Passing a Button in FXML using JavaFX

Hello I Have this portion of my code, I actually need disable the button when the user is logging, I am trying to pass the button to the FXML, but nothing happens: this is the code in the Main Controller, when the user is logged and data match with the password and username the button with the variable btn1 must disabled, I post my entire code for any help.
public Button getBtn1() {
return btn1;
}
public void conexion () {
String usus="";
String Passu ="";
String Bd="jdbc:sqlserver: // THUMANO2:1433;databaseName=QUORA";
String Usuario="sa";
String Pass="milkas87";
String SqlQuery= "select NOMBREUSUARIO, CONVERT (VARCHAR(50), (DecryptByPassPhrase('*xc/6789oÑ---+y',PASS))) as PASS from usuarios where CONVERT (VARCHAR(50), (DecryptByPassPhrase('*xc/6789oÑ---+y',PASS)))='"+fcontrasena.getText().toString().trim()+"'";
Connection Conexion = null;
try {
Conexion=DriverManager.getConnection(Bd, Usuario, Pass);
PreparedStatement ps =Conexion.prepareStatement(SqlQuery);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
usus = rs.getString("NOMBREUSUARIO");
Passu = rs.getString("PASS");
}
if(fcontrasena.getText().toString().trim().equals(Passu) && fusuario.getText().toString().equals(usus)) {
Stage administrador=new Stage();
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Admin.fxml"));
Stage login=(Stage)fusuario.getScene().getWindow();
Parent root = loader.load();
AdminScreenController controlador = loader.<AdminScreenController>getController();
controlador.setBtn1(btn1);
Scene scene=new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
administrador.setScene(scene);
administrador.setTitle("AdminScreen");
login.hide();
administrador.show();
} catch(Exception e) {}
}
} catch(SQLException e) {
JOptionPane.showMessageDialog(null,"error","ERROR",0);
}
}
this is the code for AdminScreenController:
public void setBtn1(Button btn1) {
btn1.setDisable(true);
}
this is the FXML Document:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="576.0" prefWidth="791.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AdminScreenController">
<children>
<BorderPane fx:id="bpane" prefHeight="621.0" prefWidth="791.0">
<left>
<VBox fx:id="vboxr" prefHeight="576.0" prefWidth="205.0" BorderPane.alignment="CENTER">
<children>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn1" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Planilla Sistemas" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn2" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="AMP" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="116.0" prefWidth="205.0">
<center>
<Button fx:id="btn3" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Tareas Funcionarios" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn4" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Indicadores" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0" />
</children>
</VBox>
</left>
<top>
<BorderPane prefHeight="145.0" prefWidth="791.0" BorderPane.alignment="CENTER">
<left>
<Pane fx:id="imgview" prefHeight="145.0" prefWidth="205.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>
</top>
</BorderPane>
</children>
</StackPane>
its something wrong? i need some orientation here. thanks.
Here is an example that shows the MVC relationship in JavaFX. The button is defined in FXML and injected into the controller where it can be accessed.
TestAppView.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.app.TestAppController">
<Button fx:id="myButton" text="Hello World" />
</VBox>
TestAppController.java
public class TestAppController implements Initializable {
#FXML
private Button myButton;
#Override
public void initialize(URL url, ResourceBundle resources) {
// Prints Hello World & disables the button when clicked
myButton.setOnAction((e) -> {
System.out.println("Hello World");
myButton.setDisable(true);
});
}
}
TestAppMain.java
public class TestAppMain extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(TestAppController.class.getResource("TestAppView.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 200, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFx 12 : About stages

I wonder if you can do this:
a main stage of the content
and a menu stage within that content stage
like this:
enter image description here
i try make but i got this:
enter image description here
my fxml:
<StackPane fx:id="root" prefWidth="311.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.semeq.controllers.Test">
<!-- Header -->
<BorderPane>
<top>
<VBox spacing="20">
<JFXToolbar>
<leftItems>
<JFXRippler maskType="CIRCLE" style="-fx-ripple-color:WHITE;">
<StackPane fx:id="titleBurgerContainer">
<JFXHamburger fx:id="titleBurger">
<HamburgerBackArrowBasicTransition />
</JFXHamburger>
</StackPane>
</JFXRippler>
<Label>Material Design</Label>
</leftItems>
</JFXToolbar>
</VBox>
</top>
<!-- Content Area -->
<center>
<JFXDrawer fx:id="drawer" defaultDrawerSize="250" direction="LEFT">
<styleClass>
<String fx:value="body" />
</styleClass>
</JFXDrawer>
</center>
</BorderPane>
</StackPane>
controller:
package com.semeq.controllers;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import com.jfoenix.controls.JFXDrawer;
import com.jfoenix.controls.JFXHamburger;
import com.jfoenix.controls.JFXRippler;
import com.jfoenix.transitions.hamburger.HamburgerBackArrowBasicTransition;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
#Controller
public class Test {
#FXML
private Pane root;
#FXML
private StackPane titleBurgerContainer;
#FXML
private JFXHamburger titleBurger;
#FXML
private JFXRippler optionsRippler;
#FXML
private StackPane optionsBurger;
#FXML
private VBox box;
#FXML
private JFXDrawer drawer;
public void initialize() {
try {
box = FXMLLoader.load(getClass().getResource("/Home.fxml"));
drawer.setSidePane(box);
for (Node node : box.getChildren()) {
if(node.getAccessibleText() != null) {
System.out.println("xdasdd");
node.addEventHandler(MouseEvent.MOUSE_CLICKED, (ex) -> {
switch(node.getAccessibleText()) {
case "Gerenciar" :
try {
StackPane gerenciar = FXMLLoader.load(getClass().getResource("/Gerenciar.fxml"));
root.getChildren().addAll(gerenciar);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
}
});
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HamburgerBackArrowBasicTransition transition = new HamburgerBackArrowBasicTransition(titleBurger);
drawer.setOnDrawerOpening(e -> {
transition.setRate(1);
transition.play();
});
drawer.setOnDrawerClosing(e -> {
transition.setRate(-1);
transition.play();
});
titleBurgerContainer.setOnMouseClicked(e -> {
if (drawer.isClosed() || drawer.isClosing()) {
drawer.open();
} else {
drawer.close();
}
});
}
}
i don't know if this is possible
a stage for all the content
and a stage for the menu within that content
In other words, I wanted a Main Stage
and another stage being part of this main stage and when you clicked on the main stage it would appear
Sorry to say, but you are doing it all wrong. You are making everything very complex. Whenever you work with JFXDrawer. Try to make a seperate fxml file for the drawer itself, and another fxml file for the root container, where you want your drawer to be placed. In this way you will have two fxml files. It will make things much simpler.
Here's my approach for your problem. I hope it helps you!
Main.java (Main launch file) -
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
//this is the directory - package_name/fxml_file.fxml
Parent root =
FXMLLoader.load(getClass().getResource("/application/container.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Material Design JFX Navigation Drawer");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
NavigationController.java (Controller Class) -
public class NavigationController implements Initializable {
#FXML private AnchorPane anchorPane;
#FXML private StackPane stackPane1, stackPane2, stackPane3, stackPane4;
#FXML private JFXHamburger hamburger;
#FXML private JFXDrawer drawer;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
StackPane[] stackPaneArray = {stackPane1, stackPane2, stackPane3, stackPane4};
for(int i = 0;i<stackPaneArray.length;i++){
stackPaneArray[i].setVisible(false);;
}
try {
VBox box = FXMLLoader.load(getClass().getResource("/application/drawer.fxml")); //this is the directory - package_name/fxml_file.fxml
drawer.setSidePane(box);
for(Node node : box.getChildren()){
if(node.getAccessibleText()!=null){
node.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) ->{
switch(node.getAccessibleText()){
case "Gerenciar_1":
stackPane1.setVisible(true);
stackPane2.setVisible(false);
stackPane3.setVisible(false);
stackPane4.setVisible(false);
break;
case "Gerenciar_2":
stackPane1.setVisible(false);
stackPane2.setVisible(true);
stackPane3.setVisible(false);
stackPane4.setVisible(false);
break;
case "Gerenciar_3":
stackPane1.setVisible(false);
stackPane2.setVisible(false);
stackPane3.setVisible(true);
stackPane4.setVisible(false);
break;
case "Gerenciar_4":
stackPane1.setVisible(false);
stackPane2.setVisible(false);
stackPane3.setVisible(false);
stackPane4.setVisible(true);
break;
}
});
}
}
HamburgerBackArrowBasicTransition transition = new HamburgerBackArrowBasicTransition(hamburger);
transition.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_PRESSED,(e) -> {
transition.setRate(transition.getRate()*-1);
transition.play();
if(drawer.isShown()){
drawer.close();
}else{
drawer.open();
}
});
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
container.fxml (fxml file for the container) -
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXDrawer?>
<?import com.jfoenix.controls.JFXHamburger?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="anchorPane" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="390.0"
prefWidth="460.0" xmlns="http://javafx.com/javafx/8.0.102"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.NavigationController">
<children>
<JFXDrawer fx:id="drawer" defaultDrawerSize="173.0" layoutY="24.0"
prefHeight="367.0" prefWidth="100.0" />
<MenuBar prefHeight="25.0" prefWidth="460.0">
<menus>
<Menu mnemonicParsing="false">
<graphic>
<JFXHamburger fx:id="hamburger" />
</graphic>
</Menu>
</menus>
</MenuBar>
<StackPane fx:id="stackPane1" layoutX="140.0" layoutY="25.0" prefHeight="367.0" prefWidth="320.0">
<children>
<Label text="StackPane 1">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Label>
</children></StackPane>
<StackPane fx:id="stackPane2" layoutX="140.0" layoutY="25.0" prefHeight="367.0" prefWidth="320.0">
<children>
<Label text="StackPane 2">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Label>
</children></StackPane>
<StackPane fx:id="stackPane3" layoutX="140.0" layoutY="25.0" prefHeight="367.0" prefWidth="320.0">
<children>
<Label text="StackPane 3">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Label>
</children></StackPane>
<StackPane fx:id="stackPane4" layoutX="140.0" layoutY="25.0" prefHeight="367.0" prefWidth="320.0">
<children>
<Label text="StackPane 4">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Label>
</children></StackPane>
</children>
</AnchorPane>
drawer.fxml (fxml file for the drawer) -
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox alignment="TOP_RIGHT" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="390.0"
prefWidth="173.0" xmlns="http://javafx.com/javafx/8.0.102"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<JFXButton accessibleText="Gerenciar_1" buttonType="RAISED"
focusTraversable="false" prefHeight="57.0" prefWidth="176.0"
text="Gerenciar">
<font>
<Font size="15.0" />
</font>
<graphic>
<FontAwesomeIconView glyphName="USER" size="30" wrappingWidth="43.0" />
</graphic>
</JFXButton>
<JFXButton accessibleText="Gerenciar_2" buttonType="RAISED"
focusTraversable="false" prefHeight="57.0" prefWidth="178.0"
text="Gerenciar">
<font>
<Font size="15.0" />
</font>
<graphic>
<FontAwesomeIconView glyphName="USER" size="30" wrappingWidth="43.0" />
</graphic>
</JFXButton>
<JFXButton accessibleText="Gerenciar_3" buttonType="RAISED"
focusTraversable="false" prefHeight="57.0" prefWidth="178.0"
text="Gerenciar">
<font>
<Font size="15.0" />
</font>
<graphic>
<FontAwesomeIconView glyphName="USER" size="30" wrappingWidth="43.0" />
</graphic>
</JFXButton>
<JFXButton accessibleText="Gerenciar_4" buttonType="RAISED"
focusTraversable="false" prefHeight="57.0" prefWidth="178.0"
text="Gerenciar">
<font>
<Font size="15.0" />
</font>
<graphic>
<FontAwesomeIconView glyphName="USER" size="30" wrappingWidth="43.0" />
</graphic>
</JFXButton>
</children>
</VBox>
Here's the screenshot of what I did -
Take a look. I hope it solves your problem.

Why my ToggleGroup variable is always null?

I defined the following FirstWindow.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="switcherContainer" layoutX="0.0" layoutY="0.0" prefHeight="170.0" prefWidth="200.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="rep.ButtonController">
<fx:define>
<ToggleGroup fx:id="rType"/>
</fx:define>
<Label layoutX="35.0" layoutY="25.0" prefHeight="17.0" prefWidth="260.0" text="Choose the report type:" />
<RadioButton layoutX="35.0" layoutY="60.0" mnemonicParsing="false" text="A" toggleGroup="$rType" />
<RadioButton layoutX="35.0" layoutY="80.0" mnemonicParsing="false" text="B" toggleGroup="$rType" />
<RadioButton layoutX="35.0" layoutY="100.0" mnemonicParsing="false" text="C" toggleGroup="$rType" />
<RadioButton layoutX="35.0" layoutY="120.0" mnemonicParsing="false" text="D" toggleGroup="$rType" />
</AnchorPane>
I added this FirstWindow.fxml file in Main class:
AnchorPane rootPane = (AnchorPane) FXMLLoader.load(getClass().getResource("/rep/RootScene.fxml"));
primaryStage.setTitle("Report Generation");
primaryStage.setScene(new Scene(rootPane));
AnchorPane innerPane = (AnchorPane) FXMLLoader.load(getClass().getResource("/rep/FirstWindow.fxml"));
rootPane.getChildren().addAll(innerPane);
primaryStage.show();
And my ButtonController class looks like this:
public class ButtonController implements Initializable {
static int step = 0;
#FXML
private Button nextButton;
#FXML
ToggleGroup rType;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void onNextButtonClick(ActionEvent event) {
System.out.println("Button Clicked!");
try {
if (rType != null) {
System.out.println("RadioButton selected: " + rType.getSelectedToggle().getUserData().toString());
} else {
System.out.println("rType is null");
}
...
primaryStage.show();
} catch (Exception ex) {
System.out.println("Exception: " + ex);
}
}
}
But I always get the following output in the console:
rType is null
Why? And how to fix it?

JavaFx: multiple controllers

I'm working on the project which contains multiple controllers (1 main and two for each of the Fxml files). Unfortunately while running the program, console throw the NullPointerExeption at me. I found out that it's the main controller fault, but still having this information I'm unable to fix this. Could you give me some tips how to solve this issue ?
Here i got the full track:
MainController class:
public class MainController {
#FXML LogInController logInController;
DBConnect dbConnect;
#FXML FlightControlController flightControlController;
#FXML public void initialize() {
logInController.initialize(this);
flightControlController.initialize(this);
dbConnect.initialize(this);
}
}
DBConnect class:
public void dbConnect() throws SQLException {
try {
Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/nasa", "root", "1234");
} catch(Exception e) {
System.out.println(e.getStackTrace());
}
}
public boolean isCorrect(String login,String password) throws SQLException, IOException {
Stmt = Conn.createStatement();
Rs = Stmt.executeQuery("SELECT * FROM Users WHERE login='"+login+"'AND password='"+password+"';");
if(Rs.next()) {
return true;
} else {
return false;
}
}
public void initialize(MainController mainController) {
this.mainController=mainController;
}
LogInController class:
MainController mainController;
#FXML DBConnect dbConnect;
#FXML TextField logginField = null;
#FXML PasswordField passwordFiled=null;
#FXML Button logInButton;
#FXML
public void buttonClicked(ActionEvent event) throws SQLException, IOException {
mainController.dbConnect.dbConnect();
if(mainController.dbConnect.isCorrect(logginField.getText(),passwordFiled.getText())) {
Parent root = FXMLLoader.load(getClass().getResource("/view/FlightControlView.fxml"));
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(scene);
stage.show();
} else {
System.out.print("An error have occured");
}
}
public void initialize(MainController mainController) {
this.mainController=mainController;
}
FlightControlController Class:
#FXML Label label;
MainController mainController;
public void initialize(MainController mainController) {
this.mainController = mainController;
}
And the error which occurs:
Caused by: java.lang.NullPointerException at
controller.LogInController.buttonClicked(LogInController.java:31) ...
62 more
I'm not sure but is it possible that MainController (which doesn't have his own Fxml file) so that FXMLLoader doesn't initialize MainController's properties?
#EDIT
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="693.0" prefWidth="1062.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.LogInController">
<children>
<SplitPane dividerPositions="0.21320754716981133" layoutX="331.0" layoutY="220.0" prefHeight="693.0" prefWidth="1062.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label alignment="CENTER" layoutY="-1.0" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="70.0" prefWidth="222.0" text="NASA DATABASE" />
<TextField fx:id="logginField" layoutX="18.0" layoutY="101.0" promptText="Login" />
<PasswordField fx:id="passwordFiled" layoutX="18.0" layoutY="165.0" promptText="Password" />
<Button fx:id="logInButton" onAction="#buttonClicked" layoutX="79.0" layoutY="230.0" mnemonicParsing="false" text="Log In" />
</children>
</AnchorPane>
<AnchorPane prefHeight="691.0" prefWidth="493.0" SplitPane.resizableWithParent="false">
<children>
<Label alignment="CENTER" layoutX="97.0" layoutY="104.0" prefHeight="417.0" prefWidth="635.0" text="NOT AVAILABLE, PLEASE LOG IN" AnchorPane.leftAnchor="113.0" AnchorPane.rightAnchor="113.0">
<font>
<Font name="System Bold Italic" size="37.0" />
</font>
</Label>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>

Resources