I create the following class:
package sample;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import vom.CAPlatform;
public class SecureCAController extends Application {
private double xOffset;
private double yOffset;
public CAPlatform myAgent;
public static boolean ready = false;
public SecureCAController(CAPlatform caPlatform) {
myAgent = caPlatform;
System.out.println(myAgent.getAID());
}
public SecureCAController() {
}
public void show(){
launch();
}
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
root.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
xOffset = mouseEvent.getSceneX();
yOffset = mouseEvent.getSceneY();
}
});
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
primaryStage.setX(mouseEvent.getScreenX() - xOffset);
primaryStage.setY(mouseEvent.getScreenY() - yOffset);
}
});
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setTitle("CA");
Scene sceneNew = new Scene(root);
sceneNew.setFill(Color.TRANSPARENT);
primaryStage.setScene(sceneNew);
primaryStage.show();
ready = true;
}
public static void main(String[] args) {
launch(args);
}
public void setAgent(CAPlatform caPlatform) {
myAgent = caPlatform;
}
#FXML
private ImageView userArrow;
#FXML private ImageView printerArrow;
#FXML private ImageView crudArrow;
#FXML private ImageView exitArrow;
#FXML private AnchorPane userPanel;
#FXML private AnchorPane printerPanel;
#FXML private AnchorPane crudPanel;
#FXML private JFXTextField userText;
#FXML private JFXPasswordField passwordText;
#FXML private JFXTextField AIDText;
#FXML private JFXButton startButton;
#FXML private JFXButton pendingButton;
#FXML private JFXButton validateButton;
#FXML private JFXButton validateRButton;
#FXML private JFXTextArea PList;
#FXML private JFXTextArea AreaList;
public void onstartButton(ActionEvent event){
if(userText.getText().isEmpty() || passwordText.getText().isEmpty()){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR");
alert.setContentText("PLEASE INSERT USERNAME AND PASSWORD");
}else{
System.out.println(this.myAgent.getAID());
}
}
public void onpendingButton(ActionEvent event){
}
public void onvalidateButton(ActionEvent event){
if(AIDText.getText().isEmpty()){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR");
alert.setContentText("PLEASE INSERT AN AID");
}
}
public void onvalidateRButton(ActionEvent event){
}
public void onExitButtonClicked(MouseEvent event){
Platform.exit();
System.exit(0);
}
public void onUserButtonClicked(MouseEvent event){
System.out.println("si");
userPanel.setVisible(true);
userArrow.setVisible(true);
printerPanel.setVisible(false);
crudPanel.setVisible(false);
printerArrow.setVisible(false);
crudArrow.setVisible(false);
exitArrow.setVisible(false);
}
public void onPrinterButtonClicked(MouseEvent event){
printerPanel.setVisible(true);
printerArrow.setVisible(true);
crudPanel.setVisible(false);
userPanel.setVisible(false);
userArrow.setVisible(false);
crudArrow.setVisible(false);
exitArrow.setVisible(false);
}
public void onCRUDButtonClicked(MouseEvent event){
crudPanel.setVisible(true);
crudArrow.setVisible(true);
userPanel.setVisible(false);
userArrow.setVisible(false);
printerPanel.setVisible(false);
printerArrow.setVisible(false);
exitArrow.setVisible(false);
}
}
In the secureCAController, initialize the agent and launch the application, but when i try to execute one method for example myAgent.getname() return null.
Anyone can help me? I dont know a lot of about javafx sorry if it is a dummy error.
myAgent is an object, that contains a lot of methods.
My fxml is the following:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextArea?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="474.0" prefWidth="562.0" style="-fx-background-color: #85c0cc;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SecureCAController">
<children>
<AnchorPane prefHeight="0.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;">
<children>
<HBox prefHeight="63.0" prefWidth="562.0">
<children>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onUserButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/user.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onPrinterButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/print.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onCRUDButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/accept.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onExitButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/exit.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
</children>
</HBox>
</children>
</AnchorPane>
<AnchorPane fx:id="userPanel" layoutY="86.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<children>
<Label layoutX="60.0" layoutY="126.0" prefHeight="26.0" prefWidth="120.0" text=" USER:">
<font>
<Font name="SansSerif Bold" size="20.0" />
</font>
</Label>
<Label layoutX="57.0" layoutY="191.0" text="PASSWORD:">
<font>
<Font name="SansSerif Bold Italic" size="20.0" />
</font>
</Label>
<JFXTextField fx:id="userText" layoutX="228.0" layoutY="126.0" prefHeight="27.0" prefWidth="236.0" promptText="Enter your username" />
<ImageView fitHeight="100.0" fitWidth="100.0" layoutX="220.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/shelt.png" />
</image>
</ImageView>
<JFXPasswordField fx:id="passwordText" layoutX="228.0" layoutY="189.0" prefHeight="27.0" prefWidth="236.0" promptText="Enter your password" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</AnchorPane>
<JFXButton fx:id="startButton" layoutX="203.0" layoutY="337.0" onAction="#onstartButton" prefHeight="27.0" prefWidth="157.0" style="-fx-background-color: #85c0cc;" text="Start">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</AnchorPane>
<ImageView fx:id="userArrow" fitHeight="30.0" fitWidth="90.0" layoutX="83.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="printerArrow" fitHeight="30.0" fitWidth="90.0" layoutX="208.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="crudArrow" fitHeight="30.0" fitWidth="90.0" layoutX="327.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="exitArrow" fitHeight="30.0" fitWidth="90.0" layoutX="444.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<AnchorPane fx:id="printerPanel" layoutX="10.0" layoutY="96.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<Separator layoutY="24.0" prefHeight="3.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Label layoutX="203.0" layoutY="2.0" text="Platform List" textAlignment="CENTER">
<font>
<Font name="SansSerif Regular" size="20.0" />
</font>
</Label>
<JFXTextArea fx:id="AreaList" layoutY="27.0" prefHeight="252.0" prefWidth="523.0" />
</children>
</AnchorPane>
<JFXButton fx:id="pendingButton" layoutX="20.0" layoutY="334.0" onAction="#onpendingButton" prefHeight="40.0" prefWidth="222.0" style="-fx-background-color: #85c0cc;" text="Pending Requests">
<font>
<Font size="20.0" />
</font>
</JFXButton>
<JFXButton fx:id="validateRButton" layoutX="320.0" layoutY="334.0" onAction="#onvalidateRButton" prefHeight="40.0" prefWidth="222.0" style="-fx-background-color: #85c0cc;" text="Validated Requests">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane fx:id="crudPanel" layoutX="20.0" layoutY="106.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<Separator layoutY="24.0" prefHeight="3.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Label layoutX="184.0" layoutY="2.0" text="Pending Platforms" textAlignment="CENTER">
<font>
<Font name="SansSerif Regular" size="20.0" />
</font>
</Label>
<JFXTextArea fx:id="PList" layoutY="27.0" prefHeight="252.0" prefWidth="523.0" />
</children>
</AnchorPane>
<JFXButton fx:id="ValidateButton" layoutX="322.0" layoutY="333.0" onAction="#onvalidateButton" prefHeight="40.0" prefWidth="219.0" style="-fx-background-color: #85c0cc;" text="Validate">
<font>
<Font size="20.0" />
</font>
</JFXButton>
<JFXTextField fx:id="AIDText" focusColor="BLACK" layoutX="20.0" layoutY="344.0" prefHeight="27.0" prefWidth="290.0" unFocusColor="WHITE" />
<Label layoutX="19.0" layoutY="333.0" text="PLATFORM AID:" textFill="WHITE" />
</children>
</AnchorPane>
</children>
</AnchorPane>
I have cleaned up your project a little:
Your Aplication Class:
package vom;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author mipog
*/
public class SecureCA extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("SecureCA.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Your FXML-File:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextArea?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="474.0" prefWidth="562.0" style="-fx-background-color: #85c0cc;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="vom.SecureCAController">
<children>
<AnchorPane prefHeight="0.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;">
<children>
<HBox prefHeight="63.0" prefWidth="562.0">
<children>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onUserButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/user.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onPrinterButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/print.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onCRUDButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/accept.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onExitButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/exit.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
</children>
</HBox>
</children>
</AnchorPane>
<AnchorPane fx:id="userPanel" layoutY="86.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<children>
<Label layoutX="60.0" layoutY="126.0" prefHeight="26.0" prefWidth="120.0" text=" USER:">
<font>
<Font name="SansSerif Bold" size="20.0" />
</font>
</Label>
<Label layoutX="57.0" layoutY="191.0" text="PASSWORD:">
<font>
<Font name="SansSerif Bold Italic" size="20.0" />
</font>
</Label>
<JFXTextField fx:id="userText" layoutX="228.0" layoutY="126.0" prefHeight="27.0" prefWidth="236.0" promptText="Enter your username" />
<ImageView fitHeight="100.0" fitWidth="100.0" layoutX="220.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/shelt.png" />
</image>
</ImageView>
<JFXPasswordField fx:id="passwordText" layoutX="228.0" layoutY="189.0" prefHeight="27.0" prefWidth="236.0" promptText="Enter your password" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</AnchorPane>
<JFXButton fx:id="startButton" layoutX="203.0" layoutY="337.0" onAction="#onstartButton" prefHeight="27.0" prefWidth="157.0" style="-fx-background-color: #85c0cc;" text="Start">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</AnchorPane>
<ImageView fx:id="userArrow" fitHeight="30.0" fitWidth="90.0" layoutX="83.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="printerArrow" fitHeight="30.0" fitWidth="90.0" layoutX="208.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="crudArrow" fitHeight="30.0" fitWidth="90.0" layoutX="327.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="exitArrow" fitHeight="30.0" fitWidth="90.0" layoutX="444.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<AnchorPane fx:id="printerPanel" layoutX="10.0" layoutY="96.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<Separator layoutY="24.0" prefHeight="3.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Label layoutX="203.0" layoutY="2.0" text="Platform List" textAlignment="CENTER">
<font>
<Font name="SansSerif Regular" size="20.0" />
</font>
</Label>
<JFXTextArea fx:id="AreaList" layoutY="27.0" prefHeight="252.0" prefWidth="523.0" />
</children>
</AnchorPane>
<JFXButton fx:id="pendingButton" layoutX="20.0" layoutY="334.0" onAction="#onpendingButton" prefHeight="40.0" prefWidth="222.0" style="-fx-background-color: #85c0cc;" text="Pending Requests">
<font>
<Font size="20.0" />
</font>
</JFXButton>
<JFXButton fx:id="validateRButton" layoutX="320.0" layoutY="334.0" onAction="#onvalidateRButton" prefHeight="40.0" prefWidth="222.0" style="-fx-background-color: #85c0cc;" text="Validated Requests">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane fx:id="crudPanel" layoutX="20.0" layoutY="106.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<Separator layoutY="24.0" prefHeight="3.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Label layoutX="184.0" layoutY="2.0" text="Pending Platforms" textAlignment="CENTER">
<font>
<Font name="SansSerif Regular" size="20.0" />
</font>
</Label>
<JFXTextArea fx:id="PList" layoutY="27.0" prefHeight="252.0" prefWidth="523.0" />
</children>
</AnchorPane>
<JFXButton fx:id="ValidateButton" layoutX="322.0" layoutY="333.0" onAction="#onvalidateButton" prefHeight="40.0" prefWidth="219.0" style="-fx-background-color: #85c0cc;" text="Validate">
<font>
<Font size="20.0" />
</font>
</JFXButton>
<JFXTextField fx:id="AIDText" focusColor="BLACK" layoutX="20.0" layoutY="344.0" prefHeight="27.0" prefWidth="290.0" unFocusColor="WHITE" />
<Label layoutX="19.0" layoutY="333.0" text="PLATFORM AID:" textFill="WHITE" />
</children>
</AnchorPane>
</children>
</AnchorPane>
And your Controller Class:
package vom;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
/**
*
* #author mipog
*/
public class SecureCAController implements Initializable {
#FXML
private ImageView userArrow;
#FXML
private ImageView printerArrow;
#FXML
private ImageView crudArrow;
#FXML
private ImageView exitArrow;
#FXML
private AnchorPane userPanel;
#FXML
private AnchorPane printerPanel;
#FXML
private AnchorPane crudPanel;
#FXML
private JFXTextField userText;
#FXML
private JFXPasswordField passwordText;
#FXML
private JFXTextField AIDText;
#FXML
private JFXButton startButton;
#FXML
private JFXButton pendingButton;
#FXML
private JFXButton validateButton;
#FXML
private JFXButton validateRButton;
#FXML
private JFXTextArea PList;
#FXML
private JFXTextArea AreaList;
//You can either here init your myAgent or in the initialize method
CAPlatform myAgent = new CAPlatform();
#FXML
private void onstartButton() {
if (userText.getText().isEmpty() || passwordText.getText().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR");
alert.setContentText("PLEASE INSERT USERNAME AND PASSWORD");
} else {
System.out.println(this.myAgent.getAID());
}
}
#FXML
private void onpendingButton() {
}
#FXML
private void onvalidateButton() {
if (AIDText.getText().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR");
alert.setContentText("PLEASE INSERT AN AID");
}
}
#FXML
private void onvalidateRButton() {
}
#FXML
private void onExitButtonClicked() {
Platform.exit();
System.exit(0);
}
#FXML
private void onUserButtonClicked() {
System.out.println("si");
userPanel.setVisible(true);
userArrow.setVisible(true);
printerPanel.setVisible(false);
crudPanel.setVisible(false);
printerArrow.setVisible(false);
crudArrow.setVisible(false);
exitArrow.setVisible(false);
}
#FXML
private void onPrinterButtonClicked() {
printerPanel.setVisible(true);
printerArrow.setVisible(true);
crudPanel.setVisible(false);
userPanel.setVisible(false);
userArrow.setVisible(false);
crudArrow.setVisible(false);
exitArrow.setVisible(false);
}
#FXML
private void onCRUDButtonClicked() {
crudPanel.setVisible(true);
crudArrow.setVisible(true);
userPanel.setVisible(false);
userArrow.setVisible(false);
printerPanel.setVisible(false);
printerArrow.setVisible(false);
exitArrow.setVisible(false);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// Here you init you myAgent
myAgent = new CAPlatform();
}
}
Maybe this will help you a little..
if you want to pass myAgent to the Controller Class from the Application Class. Your Application class has to look like the following. And you have to make a setter for the myAgent variable in the Controller Class.
package vom;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author mipog
*/
public class SecureCA extends Application {
CAPlatform myAgent = new CAPlatform();
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("SecureCA.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
SecureCAController controller = loader.getController();
controller.setMyAgent(myAgent);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Related
This question already has answers here:
What's the difference between fx:id and id: in JavaFX?
(4 answers)
Closed 4 years ago.
I have a JavaFX app that has the function to bring up a FileChooser. I just want to get the path of the file selected and set a Text Field to that path in the app. I need to re-use this function 4 times. Every time, I pass the TextField ID to the function it says it's null and then get the NullPointerException. I've tried several different solutions that said to create an initialize() for it, but that does not work either.
Multiple sources said to initialize the text field objects such as:
TextField global_dataset_1 = new TextField();
Then some sources said don't do that and to just refer to the .fxml IDs; which is what I thought originally to do... I tried the first one above by declaring it in the beginning and wrapping it into an #Override initialize(). Neither worked. I have the IDs set in the .fxml file.
I've also looked at: http://tutorials.jenkov.com/javafx/filechooser.html and https://examples.javacodegeeks.com/desktop-java/javafx/fxml/javafx-fxml-controller-example/ and https://github.com/mwilchek/Restaurant-Gift-Card-Lookup-App/blob/master/src/controller/NewAccountController.java
Below is what I have:
Main.java
package views;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
public static Stage primaryStage = new Stage();
#Override
public void start(Stage primaryStage) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Configuration Manager");
primaryStage.setScene(new Scene(root, 1100, 700));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FxmlController.java
package controller;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Accordion;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import java.io.File;
import static views.Main.primaryStage;
public class FxmlController {
#FXML
Accordion mainList;
#FXML
AnchorPane AnchorPane1;
//TextFields are all set to NULL ERROR
#FXML
TextField global_dataset_1;
#FXML
TextField global_dataset_2;
#FXML
TextField global_dataset_3;
#FXML
TextField global_dataset_4;
public void fileChooser1() {
fileChooser(global_dataset_1);
}
public void fileChooser2() {
fileChooser(global_dataset_2);
}
public void fileChooser3() {
fileChooser(global_dataset_3);
}
public void fileChooser4() {
fileChooser(global_dataset_4);
}
public FxmlController(){
}
// Error: For some reason won't pass TextField Object here...
public void fileChooser(TextField field) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Global Dataset");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("CSV Files", "*.csv"),
new FileChooser.ExtensionFilter("All Files", "*.*"));
File selectedFile = fileChooser.showOpenDialog(primaryStage);
field.appendText(selectedFile.getPath());
}
Main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox prefHeight="700.0" prefWidth="1100.0" style="-fx-background-color: white;" stylesheets="#bootstrap3.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.FxmlController">
<children>
<MenuBar style="-fx-background-color: #2176ff;" styleClass="context-menu" stylesheets="#bootstrap3.css" >
<menus>
<Menu mnemonicParsing="false" styleClass="menu-item" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Open…" />
<Menu mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As…" />
<MenuItem mnemonicParsing="false" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Preferences…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<SplitPane dividerPositions="0.5, 0.5, 0.5" prefHeight="659.0" prefWidth="1100.0" >
<items>
<Accordion id="mainList" prefWidth="384.0">
<panes>
<TitledPane animated="false" styleClass="primary" stylesheets="#bootstrap3.css" text="Global Configurations">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="52.0" prefWidth="393.0" >
<children>
<Label layoutX="14.0" layoutY="15.0" prefHeight="41.0" prefWidth="93.0" text="Year:">
<font>
<Font size="18.0" />
</font>
</Label>
<ChoiceBox id="year" layoutX="405.0" layoutY="21.0" prefWidth="116.0" style="-fx-background-color: #2176ff;" styleClass="primary" stylesheets="#bootstrap3.css" value="2018">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="2018" />
<String fx:value="2019" />
<String fx:value="2020" />
<String fx:value="2021" />
<String fx:value="2022" />
</FXCollections>
</items>
</ChoiceBox>
<Label layoutX="14.0" layoutY="70.0" text="Run Name:">
<font>
<Font size="18.0" />
</font>
</Label>
<Separator layoutX="-3.0" layoutY="54.0" prefHeight="13.0" prefWidth="537.0" />
<TextField id="run_name" layoutX="298.0" layoutY="68.0" prefHeight="32.0" prefWidth="225.0" promptText="Run Name" style="-fx-background-color: #2176ff; -fx-text-fill: white;" styleClass="primary" stylesheets="#bootstrap3.css">
<font>
<Font size="14.0" />
</font>
</TextField>
<Separator layoutX="-2.0" layoutY="109.0" prefHeight="13.0" prefWidth="537.0" />
<Label layoutX="14.0" layoutY="122.0" prefHeight="35.0" prefWidth="140.0" text="Global Datasets" underline="true">
<font>
<Font name="Century" size="18.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="160.0" text="Dataset 1: ">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="192.0" text="Dataset 2: ">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="227.0" text="Dataset 3: ">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="263.0" text="Dataset 4: ">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField id="global_dataset_1" cache="true" layoutX="111.0" layoutY="158.0" prefWidth="379.0" promptText="Path to File" styleClass="primary" stylesheets="#bootstrap3.css" />
<Button layoutX="497.0" layoutY="157.0" mnemonicParsing="false" onMouseClicked="#fileChooser1" styleClass="primary" stylesheets="#bootstrap3.css" text="..." />
<TextField id="global_dataset_2" cache="true" layoutX="111.0" layoutY="191.0" prefWidth="379.0" promptText="Path to File" styleClass="primary" stylesheets="#bootstrap3.css" />
<TextField id="global_dataset_3" cache="true" layoutX="111.0" layoutY="227.0" prefWidth="379.0" promptText="Path to File" styleClass="primary" stylesheets="#bootstrap3.css" />
<TextField id="global_dataset_4" cache="true" layoutX="110.0" layoutY="263.0" prefWidth="379.0" promptText="Path to File" styleClass="primary" stylesheets="#bootstrap3.css" />
<Button layoutX="497.0" layoutY="192.0" mnemonicParsing="false" styleClass="primary" stylesheets="#bootstrap3.css" text="..." />
<Button layoutX="497.0" layoutY="226.0" mnemonicParsing="false" styleClass="primary" stylesheets="#bootstrap3.css" text="..." />
<Button layoutX="497.0" layoutY="262.0" mnemonicParsing="false" styleClass="primary" stylesheets="#bootstrap3.css" text="..." />
</children></AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" styleClass="primary" stylesheets="#bootstrap3.css" text="Annual 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</TitledPane>
<TitledPane animated="false" styleClass="primary" stylesheets="#bootstrap3.css" text="Annual 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</TitledPane>
</panes>
</Accordion>
<AnchorPane id="AnchorPane1" prefHeight="200.0" prefWidth="200.0" visible="true" />
<AnchorPane id="AnchorPane2" prefHeight="200.0" prefWidth="200.0" visible="false" />
<AnchorPane id="AnchorPane3" prefHeight="200.0" prefWidth="200.0" visible="false" />
</items>
</SplitPane>
</children>
</VBox>
Any help would be greatly appreciated!
You have errors with how you've built your FXML file. Within your tags, you need to set the node's ID using fx:id but you only do id.
Your TextFields should be changed to:
<TextField fx:id="global_dataset_1" ...
A couple side notes:
You may want to use field.setText() instead of appendText() unless your intention is to allow them to select multiple files and list them all in the same text field.
Also, in your controller class, you should declare your nodes as private:
#FXML
private TextField global_dataset_1;
Lastly, sharing your primaryStage as a public field is not necessary (or recommended). Another way to open the FileChooser centered would be to get the current Window from any node declared in your controller:
File selectedFile = fileChooser.showOpenDialog(
global_dataset_1.getScene().getWindow());
I am trying to add listener to the textfield in the controler but cudnt do it . i coudnt find any options of textfield in the controler while using the fx:id given to that specific text field.
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.effect.Reflection?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="294.0" prefWidth="883.0" style="-fx-background-color: #000000;" stylesheets="#style.css" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler">
<children>
<Label layoutX="28.0" layoutY="55.0" text="Enter Text : " AnchorPane.leftAnchor="28.0">
<font>
<Font size="15.0" />
</font>
</Label>
<TextField fx:id="msg_tb" alignment="CENTER" layoutX="117.0" layoutY="52.0" prefHeight="25.0" prefWidth="342.0" style="-fx-background-radius: 10;" stylesheets="#style.css" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="424.0" />
<TextField fx:id="n_msg_tb" alignment="CENTER" layoutX="117.0" layoutY="93.0" prefHeight="25.0" prefWidth="342.0" style="-fx-background-radius: 10;" stylesheets="#style.css" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="424.0" />
<Button fx:id="reset_b" alignment="CENTER" layoutX="602.0" layoutY="171.0" mnemonicParsing="false" onAction="#reset" style="-fx-text-fill: #FFFFFF; -fx-background-radius: 20;" stylesheets="#style.css" text="RESET" AnchorPane.rightAnchor="161.0">
<font>
<Font name="Calibri Bold" size="31.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
<VBox layoutX="495.0" layoutY="31.0" prefHeight="169.0" prefWidth="167.0" spacing="3.0" stylesheets="#style.css" AnchorPane.rightAnchor="221.0">
<children>
<RadioButton fx:id="er" mnemonicParsing="false" onAction="#er_action" prefHeight="17.0" prefWidth="92.0" stylesheets="#application.css" text="ENCRIPTION" textFill="#797979">
<toggleGroup>
<ToggleGroup fx:id="group1" />
</toggleGroup>
</RadioButton>
<AnchorPane fx:id="ebox" prefHeight="150.0" prefWidth="167.0">
<children>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="7.0" layoutY="2.0" prefHeight="25.0" prefWidth="131.0" stylesheets="#application.css" text="NUMBER LETTER">
<graphic>
<Button minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_nl" prefHeight="15.0" prefWidth="15.0" text="*">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="60.0" layoutX="8.0" layoutY="27.0" prefHeight="25.0" prefWidth="131.0" stylesheets="#application.css" text="AT-BASH">
<graphic>
<Button graphicTextGap="0.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_at" prefHeight="15.0" prefWidth="15.0" text="Button">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="8.0" layoutY="52.0" prefHeight="25.0" prefWidth="105.0" stylesheets="#style.css" text="CEASER">
<graphic>
<TextField fx:id="e_key_tb" onAction="#e_c_key_tb" prefHeight="25.0" prefWidth="40.0" promptText="KEY" style="-fx-background-radius: 10;" stylesheets="#style.css" />
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Button alignment="CENTER" disable="true" layoutX="110.0" layoutY="67.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_c" prefHeight="25.0" prefWidth="40.0" style="-fx-background-radius: 10;" stylesheets="#application.css" text="CEASER" textAlignment="CENTER">
<font>
<Font name="System Italic" size="8.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</children>
</AnchorPane>
</children>
</VBox>
<VBox layoutX="662.0" layoutY="31.0" prefHeight="169.0" prefWidth="167.0" spacing="3.0" stylesheets="#style.css" AnchorPane.rightAnchor="54.0">
<children>
<RadioButton fx:id="dr" mnemonicParsing="false" onAction="#dr_action" prefHeight="17.0" prefWidth="98.0" stylesheets="#application.css" text="DECRIPTION" textFill="#797979" toggleGroup="$group1" />
<AnchorPane fx:id="dbox" prefHeight="150.0" prefWidth="167.0" visible="false">
<children>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="7.0" layoutY="2.0" prefHeight="25.0" prefWidth="131.0" stylesheets="#application.css" text="NUMBER LETTER">
<graphic>
<Button minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="15.0" prefWidth="15.0" text="*">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="60.0" layoutX="8.0" layoutY="27.0" prefHeight="25.0" prefWidth="131.0" stylesheets="#application.css" text="AT-BASH">
<graphic>
<Button graphicTextGap="0.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="15.0" prefWidth="15.0" text="Button">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="8.0" layoutY="52.0" prefHeight="25.0" prefWidth="105.0" stylesheets="#style.css" text="CEASER">
<graphic>
<TextField fx:id="d_key_tb" onAction="#d_c_key_tb" prefHeight="25.0" prefWidth="40.0" promptText="KEY" style="-fx-background-radius: 10;" stylesheets="#style.css" />
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Button disable="true" layoutX="110.0" layoutY="67.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#d_c" prefHeight="25.0" prefWidth="40.0" style="-fx-background-radius: 10;" stylesheets="#application.css" text="CEASER" textAlignment="CENTER">
<font>
<Font name="System Italic" size="8.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</children>
</AnchorPane>
</children>
</VBox>
</children>
</fx:root>
controler.java
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
public class controler implements Initializable {
#FXML
private TextField msg_tb;
#FXML
private TextField n_msg_tb;
#FXML
private Button reset_b;
#FXML
private RadioButton er;
#FXML
private ToggleGroup group1;
#FXML
private RadioButton dr;
#FXML
private AnchorPane ebox;
#FXML
private AnchorPane dbox;
#FXML
public TextField e_key_tb;
#FXML
private TextField d_key_tb;
#FXML
void d_c(ActionEvent event) {
}
#FXML
void d_c_key_tb(ActionEvent event) {
}
#FXML
void dr_action(ActionEvent event) {
dbox.setVisible(true);
dbox.setDisable(false);
ebox.setVisible(false);
ebox.setDisable(true);
}
#FXML
void e_at(ActionEvent event) {
}
#FXML
void e_c(ActionEvent event) {
}
#FXML
void e_c_key_tb(ActionEvent event) {
}
#FXML
void e_nl(ActionEvent event) {
}
#FXML
void er_action(ActionEvent event) {
ebox.setVisible(true);
ebox.setDisable(false);
dbox.setVisible(false);
dbox.setDisable(true);
}
#FXML
void reset(ActionEvent event) {
}
#Override
public void initialize(URL location, ResourceBundle resources) {
ArrayList<morse> mkey = new ArrayList<>();
try {
morse.load(mkey);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public TextField getMsg_tb() {
return msg_tb;
}
public void setMsg_tb(TextField msg_tb) {
this.msg_tb = msg_tb;
}
public TextField getN_msg_tb() {
return n_msg_tb;
}
public void setN_msg_tb(TextField n_msg_tb) {
this.n_msg_tb = n_msg_tb;
}
public Button getReset_b() {
return reset_b;
}
public void setReset_b(Button reset_b) {
this.reset_b = reset_b;
}
public RadioButton getEr() {
return er;
}
public void setEr(RadioButton er) {
this.er = er;
}
public ToggleGroup getGroup1() {
return group1;
}
public void setGroup1(ToggleGroup group1) {
this.group1 = group1;
}
public RadioButton getDr() {
return dr;
}
public void setDr(RadioButton dr) {
this.dr = dr;
}
public AnchorPane getEbox() {
return ebox;
}
public void setEbox(AnchorPane ebox) {
this.ebox = ebox;
}
public AnchorPane getDbox() {
return dbox;
}
public void setDbox(AnchorPane dbox) {
this.dbox = dbox;
}
public TextField getE_key_tb() {
return e_key_tb;
}
public void setE_key_tb(TextField e_key_tb) {
this.e_key_tb = e_key_tb;
}
public TextField getD_key_tb() {
return d_key_tb;
}
public void setD_key_tb(TextField d_key_tb) {
this.d_key_tb = d_key_tb;
}
e_key_tb.textProperty().addListener((obs, oldText, newText) -> {
System.out.println("Text changed from "+ oldText +" to "+newText);
});
}
}
when I am trying to add the listener at the end it is not working. I can't get any options for textfield when I do e_key_tb. and ctrl+space where it is supposed to give a drop down box with a bunch of suggestions ... I used the scene-builder to provide id and copied the controller skeliton from the scene-builder ... later when I'm trying to use the textfield it made me create getters and setters for all the textfields can you explain me y it s happening and I never used listeners before.
Here is a short demonstration of adding a listener to a TextField. This can also serve as an example for mcve for the issue:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.TextField?>
<HBox prefHeight="75.0" prefWidth="150.0" alignment="CENTER"
xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tests.Controler">
<children>
<TextField fx:id="msg_tb" prefHeight="25.0" prefWidth="100.0" style="-fx-background-radius: 10;" />
</children>
</HBox>
The controller :
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
public class Controler implements Initializable {
#FXML
private TextField msg_tb;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
msg_tb.textProperty().addListener((obs, oldText, newText) -> {
System.out.println("Text changed from "+ oldText +" to "+newText);
});
}
}
Test it:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class FxmlMain extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Pane root = FXMLLoader.load(getClass().getResource("xml/FxmlMain.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) { launch(args);}
}
I am having trouble displaying the combo box options in javafx and scene builder.
First I have tried to do use an array, which I guess does not work. I then tried to use an ObservableList<String> and create an instance of the combo box like
ComboBox combo = new ComboBox(list);
This still would not show the combobox options.
Controller class:
package javafxapplication1;
import javafxapplication1.JavaFXApplication1;
import java.net.URL;
import static java.util.Collections.list;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javax.lang.model.element.Element;
/**
*
* #author KJ4CC
*/
public class FXMLDocumentController implements Initializable {
ObservableList<String> options =
FXCollections.observableArrayList(
"Option 1",
"Option 2",
"Option 3"
);
#FXML
private Label label;
#FXML
private TextField dateText;
#FXML
private TextField time;
#FXML
private ComboBox band;
public void setTimeDate(){
JavaFXApplication1 javaFXApp = new JavaFXApplication1();
dateText.setText(javaFXApp.getDate());
}
public void setTime(){
JavaFXApplication1 javaFXApp = new JavaFXApplication1();
time.setText(javaFXApp.getTime());
}
#Override
public void initialize(URL url, ResourceBundle rb) {
band = new ComboBox(options);
}
}
Main java class:
public class JavaFXApplication1 extends Application {
private FXMLDocumentController initScene;
private DateFormat dtf;
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public String getDate(){
dtf = new SimpleDateFormat("dd/MM/yy");
Date dateobj = new Date();
return dtf.format(dateobj);
}
public String getTime(){
dtf = new SimpleDateFormat("HH:mm;ss");
Date dateobj = new Date();
return dtf.format(dateobj);
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="383.0" prefWidth="590.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController">
<top>
<VBox prefHeight="235.0" prefWidth="543.0" BorderPane.alignment="CENTER">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TableView prefHeight="210.0" prefWidth="634.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
<VBox.margin>
<Insets left="2.0" right="2.0" />
</VBox.margin>
</TableView>
</children>
</VBox>
</top>
<bottom>
<VBox prefHeight="160.0" prefWidth="676.0" BorderPane.alignment="CENTER">
<children>
<HBox prefHeight="22.0" prefWidth="625.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Date:" wrappingWidth="83.462890625">
<HBox.margin>
<Insets left="5.0" right="20.0" />
</HBox.margin>
</Text>
<Label onMouseClicked="#setTimeDate" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Time:" wrappingWidth="73.40625" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Call:" wrappingWidth="122.94921875">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Band:" wrappingWidth="58.443359375" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Freq:" wrappingWidth="106.974609375" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Location:" />
</children>
</HBox>
<HBox prefHeight="35.0" prefWidth="625.0">
<children>
<TextField id="dateText" fx:id="dateText" prefHeight="25.0" prefWidth="94.0">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</TextField>
<TextField fx:id="time" layoutX="10.0" layoutY="10.0" prefHeight="25.0" prefWidth="71.0">
<padding>
<Insets left="5.0" />
</padding>
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</TextField>
<TextField layoutX="104.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<ComboBox fx:id="band" prefHeight="25.0" prefWidth="57.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</ComboBox>
<TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0" />
</children>
</HBox>
<HBox prefHeight="30.0" prefWidth="580.0">
<children>
<Text fill="#14bdd7" onMouseClicked="#setTimeDate" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Date">
<HBox.margin>
<Insets right="20.0" />
</HBox.margin>
</Text>
<Text fill="#14bdd7" layoutX="10.0" layoutY="23.0" onMouseClicked="#setTime" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Time" />
</children>
</HBox>
</children>
<BorderPane.margin>
<Insets left="5.0" right="5.0" />
</BorderPane.margin>
</VBox>
</bottom>
</BorderPane>
Any help would be appreciated! I am kind of new to this scene builder stuff and using javafx. I am just feeling my way around it. Thanks for the help!
Ok so i figured out the problem. I was creating a new instance of a combo box thus creating an empty one. Here is the method with the revised working code.
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> options =
FXCollections.observableArrayList(
"Option 1",
"Option 2",
"Option 3"
);
band.setItems(options);
}
This question already has answers here:
Javafx tableview not showing data in all columns
(3 answers)
Javafx PropertyValueFactory not populating Tableview
(2 answers)
Closed 5 years ago.
I am trying to build 3 different tables the will be be independent from each other. For some reason the cells are showing but are blank. Any idea what I am doing wrong?
Main:
package application;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("GUISCENEBUILDER.fxml"));
Scene scene = new Scene(root,850,700);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Switch Utility");
primaryStage.show();
} catch(Exception e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE,null,e);
e.printStackTrace();
}
}
public static void main(String[] args) {
Application.launch(Main.class, (java.lang.String[]) null);
}
}
Product class: (Bad naming convention) (Going to change it to Press1)
package application;
import javafx.beans.property.SimpleStringProperty;
public class Product {
private final SimpleStringProperty windowTime;
public Product(String windowTimes) {
this.windowTime = new SimpleStringProperty(windowTimes);
}
public String getwindowTime() {
return windowTime.get();
}
public void setwindowTime(String newTime) {
windowTime.set(newTime);
}
Press2 class:
package application;
import javafx.beans.property.SimpleStringProperty;
public class Press2 {
private final SimpleStringProperty windowTime2;
public Press2(String windowTimes2) {
this.windowTime2 = new SimpleStringProperty(windowTimes2);
}
public String getwindowTime2() {
return windowTime2.get();
}
public void setwindowTime2(String newTime) {
windowTime2.set(newTime);
}
}
Press3:
package application;
import javafx.beans.property.SimpleStringProperty;
public class Press3 {
private final SimpleStringProperty windowTime3;
public Press3(String windowTimes3) {
this.windowTime3 = new SimpleStringProperty(windowTimes3);
}
public String getwindowTime2() {
return windowTime3.get();
}
public void setwindowTime2(String newTime) {
windowTime3.set(newTime);
}
}
TableViewController:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class TableViewController implements Initializable {
//Defining table
#FXML
private TableView<Product> tableID;
#FXML
private TableView<Press2> tableID2;
#FXML
private TableView<Press3> tableID3;
#FXML
private TableColumn<Product, String> windowTime;
#FXML
private TableColumn<Press2, String> windowTime2;
#FXML
private TableColumn<Press3, String> windowTime3;
/*
* Creating all the table data
*/
final ObservableList<Product> data = FXCollections.observableArrayList(
new Product("7:00am - 8:30am"),
new Product("10:00am - 11:10am"),
new Product("12:00pm - 1:30pm"),
new Product("3:00pm - 4:30pm")
);
final ObservableList<Press2> data2 = FXCollections.observableArrayList(
new Press2("8:00am - 8:30am"),
new Press2("10:00am - 11:10am"),
new Press2("12:00pm - 1:30pm"),
new Press2("3:00pm - 4:30pm")
);
final ObservableList<Press3> data3 = FXCollections.observableArrayList(
new Press3("9:00am - 8:30am"),
new Press3("10:00am - 11:10am"),
new Press3("12:00pm - 1:30pm"),
new Press3("3:00pm - 4:30pm")
);
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
#Override
public void initialize(URL location, ResourceBundle resources) {
windowTime.setCellValueFactory(new PropertyValueFactory<Product, String>("windowTime"));
windowTime2.setCellValueFactory(new PropertyValueFactory<Press2, String>("windowTime2"));
windowTime3.setCellValueFactory(new PropertyValueFactory<Press3, String>("windowTime3"));
tableID.setItems(data);
tableID2.setItems(data2);
tableID3.setItems(data3);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.paint.LinearGradient?>
<?import javafx.scene.paint.Stop?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="850.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TableViewController">
<center>
<TabPane prefHeight="472.0" prefWidth="850.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab text="Indigo">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="526.0" prefWidth="805.0">
<children>
<ImageView fitHeight="559.0" fitWidth="850.0" layoutY="-7.0">
<image>
<Image url="#../../bin/application/1.jpg" />
</image>
</ImageView>
<TableView fx:id="tableID" layoutX="651.0" layoutY="44.0" prefHeight="237.0" prefWidth="134.0">
<columns>
<TableColumn fx:id="windowTime" prefWidth="75.0" text="Window Times" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<TextField layoutX="14.0" layoutY="55.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Output\Ready for Print" />
<Button layoutX="501.0" layoutY="55.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="87.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\CMYK+Spot" />
<Button layoutX="501.0" layoutY="87.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="21.0" text="Connection 1:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="166.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Output\Ready for Print" />
<Button layoutX="501.0" layoutY="166.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="198.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\CMYK+Spot" />
<Button layoutX="501.0" layoutY="198.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="132.0" text="Connection 2:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="276.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Output\Ready for Print" />
<Button layoutX="501.0" layoutY="276.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="308.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\CMYK+Spot" />
<Button layoutX="501.0" layoutY="308.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="242.0" text="Connection 3:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="387.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Output\Ready for Print" />
<Button layoutX="501.0" layoutY="387.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="419.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\CMYK+Spot" />
<Button layoutX="501.0" layoutY="419.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="353.0" text="Connection 4:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="iGen">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<ImageView fitHeight="561.0" fitWidth="850.0" layoutY="-7.0">
<image>
<Image url="#../../bin/application/1.jpg" />
</image>
</ImageView>
<TableView fx:id="tableID2" layoutX="651.0" layoutY="44.0" prefHeight="237.0" prefWidth="134.0">
<columns>
<TableColumn fx:id="windowTime2" prefWidth="75.0" text="Window Times" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<TextField layoutX="14.0" layoutY="55.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Envelopes\10x13 for Press" />
<Button layoutX="501.0" layoutY="55.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="87.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\10 x 13 Envelopes" />
<Button layoutX="501.0" layoutY="87.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="21.0" text="Connection 1:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="166.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Envelopes\10x13 for Press" />
<Button layoutX="501.0" layoutY="166.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="198.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\10 x 13 Envelopes" />
<Button layoutX="501.0" layoutY="198.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="132.0" text="Connection 2:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="276.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Envelopes\10x13 for Press" />
<Button layoutX="501.0" layoutY="276.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="308.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\10 x 13 Envelopes" />
<Button layoutX="501.0" layoutY="308.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="242.0" text="Connection 3:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="387.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Envelopes\10x13 for Press" />
<Button layoutX="501.0" layoutY="387.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="419.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\10 x 13 Envelopes" />
<Button layoutX="501.0" layoutY="419.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="353.0" text="Connection 4:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Memjet">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<ImageView fitHeight="559.0" fitWidth="850.0" layoutY="-7.0">
<image>
<Image url="#../../bin/application/1.jpg" />
</image>
</ImageView>
<TableView fx:id="tableID3" layoutX="651.0" layoutY="44.0" prefHeight="237.0" prefWidth="134.0">
<columns>
<TableColumn fx:id="windowTime3" prefWidth="75.0" text="Window Times" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<TextField layoutX="14.0" layoutY="55.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Envelopes\10x13 for Press" />
<Button layoutX="501.0" layoutY="55.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="87.0" prefHeight="25.0" prefWidth="475.0" promptText="A:\" />
<Button layoutX="501.0" layoutY="87.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="21.0" text="Connection 1:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="166.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Envelopes\10x13 for Press" />
<Button layoutX="501.0" layoutY="166.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="198.0" prefHeight="25.0" prefWidth="475.0" promptText="A:\" />
<Button layoutX="501.0" layoutY="198.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="132.0" text="Connection 2:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="276.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Envelopes\10x13 for Press" />
<Button layoutX="501.0" layoutY="276.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="308.0" prefHeight="25.0" prefWidth="475.0" promptText="J:\10 x 13 Envelopes" />
<Button layoutX="501.0" layoutY="308.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="242.0" text="Connection 3:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
<TextField layoutX="14.0" layoutY="387.0" prefHeight="25.0" prefWidth="475.0" promptText="C:\Users\ASUprint\Desktop\Enfocus\Switch\ASU Print Online\Envelopes\10x13 for Press" />
<Button layoutX="501.0" layoutY="387.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<TextField layoutX="14.0" layoutY="419.0" prefHeight="25.0" prefWidth="475.0" promptText="A:\" />
<Button layoutX="501.0" layoutY="419.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0" text="Browse" />
<Label layoutX="14.0" layoutY="353.0" text="Connection 4:" underline="true">
<font>
<Font name="Lucida Sans Demibold" size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</center>
<top>
<Pane prefHeight="126.0" prefWidth="850.0" BorderPane.alignment="CENTER">
<children>
<ImageView fitHeight="126.0" fitWidth="850.0">
<image>
<Image url="#../../bin/application/1.1.jpg" />
</image>
</ImageView>
<Button layoutX="489.0" layoutY="23.0" mnemonicParsing="false" style="-fx-background-color: #C9CED9;" text="Start">
<font>
<Font size="38.0" />
</font>
<effect>
<InnerShadow blurType="ONE_PASS_BOX" color="WHITE" height="27.06" radius="6.265" width="0.0">
<input>
<DropShadow blurType="TWO_PASS_BOX" />
</input></InnerShadow>
</effect>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
<textFill>
<LinearGradient cycleMethod="REFLECT" endX="0.8523809523809524" startX="0.8333333333333334" startY="0.5523809523809524">
<stops>
<Stop color="#0a9e3b" />
<Stop color="#455445" offset="0.9961685823754789" />
<Stop color="#455445" offset="1.0" />
</stops>
</LinearGradient>
</textFill>
</Button>
<Button depthTest="ENABLE" layoutX="639.0" layoutY="23.0" mnemonicParsing="false" style="-fx-background-color: #C9CED9;" text="Stop">
<font>
<Font size="38.0" />
</font>
<effect>
<InnerShadow blurType="ONE_PASS_BOX" color="WHITE" height="27.06" radius="6.265" width="0.0">
<input>
<DropShadow blurType="TWO_PASS_BOX" />
</input></InnerShadow>
</effect>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
<textFill>
<LinearGradient cycleMethod="REFLECT" endX="0.8523809523809524" startX="0.8333333333333334" startY="0.5523809523809524">
<stops>
<Stop color="#c33838" />
<Stop color="#455445" offset="0.9961685823754789" />
<Stop color="#455445" offset="1.0" />
</stops>
</LinearGradient>
</textFill>
</Button>
<ImageView fitHeight="118.0" fitWidth="300.0" layoutX="14.0" layoutY="5.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../bin/application/ASU_PrintImagingLab_Horiz_RGB_MaroonGold_150ppi.png" />
</image>
</ImageView>
</children>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</Pane>
</top>
</BorderPane>
I'm building an application using JavaFx and scene builder, however everything works fine, except when I add the Controller class.
I get the following error:
Exception in Application start method Exception in thread "main"
java.lang.RuntimeException: Exception in Application start method at
com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at
com.sun.javafx.application.LauncherImpl$$Lambda$1/868693306.run(Unknown
Source) at java.lang.Thread.run(Thread.java:745) Caused by:
javafx.fxml.LoadException:
/C:/Users/M%20ROSE/Documents/Info%20Trivia/out/production/Info%20Trivia/sample/gameScene1.fxml:15
However the moment I remove the fx:controller attribute from this line in my fxml code it works perfectly.
<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
Here are the relevant codes
Controller Class
package sample;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.* ;
import javafx.geometry.* ;
import javafx.scene.text.* ;
import javafx.scene.image.* ;
import javafx.scene.control.* ;
import java.lang.* ;
import javafx.scene.layout.* ;
import javafx.geometry.Insets ;
import javafx.scene.layout.GridPane ;
import javafx.scene.control.Button ;
import javafx.scene.control.Label ;
public class Controller {
//Initialize fxml controls
public Button trueButton;
public Button falseButton;
public Label playerLabel;
public Label questionLabel;
public Label scoreValue;
public ImageView questionImage;
public Rectangle redBar;
public Rectangle greenBar;
//Create array for level 1 questions
String[][] levelOneData = {
{"This is a Sequence Diagram","f", null},
{"This diagram is for a database","t", null},
{"This is a rack diagram","t", null},
{"This is a flow chart","f", null},
{"This is a kind of UML diagram","t", null}
};
Image[] levelOneImages = new Image[] {
new Image("res/images/l1q1.png"),
new Image("res/images/l1q2.png"),
new Image("res/images/l1q3.png"),
new Image("res/images/l1q4.png"),
new Image("res/images/l1q5.png")
};
public void levelOneInitializer(){
questionLabel.setText(levelOneData[0][0]);
questionImage.setImage(levelOneImages[0]);
System.out.println("done");
}
}
Main Class
package sample;
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("gameScene1.fxml"));
primaryStage.setTitle("Info Trivia");
primaryStage.setScene(new Scene(root, 800, 450));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
gameScene1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<top>
<HBox styleClass="questheader" BorderPane.alignment="CENTER">
<children>
<ImageView pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#res/images/userICon.png" />
</image>
</ImageView>
<Label id="playerName" fx:id="playerLabel" text="Player 1" textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
<Region prefHeight="0.0" prefWidth="382.0" />
<Label id="ScoreLabel" layoutX="40.0" layoutY="10.0" text="Score: " textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
<Label id="scoreValue" fx:id="scoreValue" layoutX="129.0" layoutY="10.0" text="0" textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
</children>
<BorderPane.margin>
<Insets left="50.0" right="50.0" />
</BorderPane.margin>
<padding>
<Insets bottom="10.0" left="15.0" right="15.0" top="10.0" />
</padding>
</HBox>
</top>
<left>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets left="50.0" />
</BorderPane.margin>
<children>
<ImageView fx:id="questionImage" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#res/images/blankimage.png" />
</image>
<StackPane.margin>
<Insets left="35.0" />
</StackPane.margin>
</ImageView>
</children>
</StackPane>
</left>
<bottom>
<HBox BorderPane.alignment="CENTER">
<children>
<Rectangle fx:id="greenBar" arcHeight="5.0" arcWidth="5.0" fill="#34b316" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="10.0" />
<Rectangle fx:id="redBar" arcHeight="5.0" arcWidth="5.0" fill="#a93535" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="620.0" />
</children>
<padding>
<Insets bottom="40.0" left="85.0" right="85.0" />
</padding>
</HBox>
</bottom>
<center>
<BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets right="85.0" />
</BorderPane.margin>
<top>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Label fx:id="questionLabel" text="This Is a Question" textFill="#191919">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
</children>
</StackPane>
</top>
<center>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="trueButton" mnemonicParsing="false" prefWidth="100.0" text="True">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<Region prefWidth="200.0" />
<Button fx:id="falseButton" mnemonicParsing="false" prefWidth="100.0" text="False" />
</children>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
<padding>
<Insets left="20.0" top="20.0" />
</padding>
</HBox>
</center>
</BorderPane>
</center>
</BorderPane>
You controller have to implement Initializable, Try with the following :
public class Controller implement Initializable{
//Initialize fxml controls
#FXML
public Button trueButton;
#FXML
public Button falseButton;
#FXML
public Label playerLabel;
#FXML
public Label questionLabel;
#FXML
public Label scoreValue;
#FXML
public ImageView questionImage;
#FXML
public Rectangle redBar;
#FXML
public Rectangle greenBar;
//Create array for level 1 questions
String[][] levelOneData = {
{"This is a Sequence Diagram","f", null},
{"This diagram is for a database","t", null},
{"This is a rack diagram","t", null},
{"This is a flow chart","f", null},
{"This is a kind of UML diagram","t", null}
};
Image[] levelOneImages = new Image[] {
new Image("res/images/l1q1.png"),
new Image("res/images/l1q2.png"),
new Image("res/images/l1q3.png"),
new Image("res/images/l1q4.png"),
new Image("res/images/l1q5.png")
};
public void levelOneInitializer(){
questionLabel.setText(levelOneData[0][0]);
questionImage.setImage(levelOneImages[0]);
System.out.println("done");
}
}