FXML Exception error while running my code - java

This is my main inside application package:
package application;
import java.net.URL;
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.Model;
public class Main extends Application {
Parent root;
Scene scene;
#Override
public void start(Stage primaryStage) {
try {
URL url = getClass().getResource("/view/AddCustomer.fxml");
root = FXMLLoader.load(url);
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(e -> {
int save = JOptionPane.showConfirmDialog(null, "Do you want to save changes in the product table?");
if (save == 0) {
Model.getInstance().SaveCustomers();
Model.getInstance().SaveSessions();
Model.getInstance().SaveAdministrators();
Model.getInstance().SaveCoachs();
}
System.exit(0);
});
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
This is my addCustomerController inside controller package:
package controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
public class AddCustomerController {
#FXML
private Button cancelMemberButton;
#FXML
private Pane customerPane;
#FXML
private RadioButton dailyMemberType;
#FXML
private TextField memberDiscountField;
#FXML
private RadioButton memberFemaleButton;
#FXML
private TextField memberIdField;
#FXML
private RadioButton memberMaleButton;
#FXML
private TextField memberMobileField;
#FXML
private TextField memberNameField;
#FXML
private TextField memberNationalityField;
#FXML
private RadioButton monthlyMemberType;
#FXML
private Button saveMemberButton;
#FXML
private RadioButton yearlyMemberType;
#FXML
void cancelButtonClick(ActionEvent event) {
}
#FXML
void saveButtonClick(ActionEvent event) {
}
}
This is the FXML file inside view package(Its called AddCustomer.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import com.gluonhq.charm.glisten.control.ToggleButtonGroup?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane fx:id="customerPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="532.0" prefWidth="789.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.AddCustomerController">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutX="258.0" layoutY="34.0" prefHeight="41.0" prefWidth="274.0" text="Add Customer" textFill="#860b0b">
<font>
<Font name="Arial" size="37.0" />
</font>
</Label>
<Separator layoutY="98.0" prefHeight="1.0" prefWidth="789.0" style="-fx-background-color: #860b0b;">
<opaqueInsets>
<Insets />
</opaqueInsets>
</Separator>
<Label layoutX="27.0" layoutY="144.0" prefHeight="23.0" prefWidth="103.0" text="Member ID" />
<Label layoutX="27.0" layoutY="195.0" prefHeight="23.0" prefWidth="103.0" text="Name" />
<Label layoutX="27.0" layoutY="243.0" prefHeight="23.0" prefWidth="103.0" text="Mobile" />
<Label layoutX="27.0" layoutY="296.0" prefHeight="23.0" prefWidth="103.0" text="Nationality" />
<Label layoutX="27.0" layoutY="345.0" prefHeight="23.0" prefWidth="103.0" text="Gender" />
<Label layoutX="27.0" layoutY="394.0" prefHeight="23.0" prefWidth="103.0" text="MemberShip Type" />
<Label layoutX="27.0" layoutY="440.0" prefHeight="23.0" prefWidth="103.0" text="Discount" />
<TextField fx:id="memberIdField" layoutX="246.0" layoutY="143.0" prefHeight="30.0" prefWidth="486.0" />
<TextField fx:id="memberNameField" layoutX="246.0" layoutY="194.0" prefHeight="25.0" prefWidth="486.0" />
<TextField fx:id="memberMobileField" layoutX="246.0" layoutY="242.0" prefHeight="25.0" prefWidth="486.0" />
<TextField fx:id="memberNationality" layoutX="246.0" layoutY="295.0" prefHeight="25.0" prefWidth="486.0" />
<RadioButton fx:id="memberMaleButton" layoutX="341.0" layoutY="348.0" mnemonicParsing="false" text="Male" />
<RadioButton fx:id="memberFemaleButton" layoutX="412.0" layoutY="348.0" mnemonicParsing="false" text="Female" />
<RadioButton fx:id="dailyMemberType" layoutX="341.0" layoutY="397.0" mnemonicParsing="false" text="Daily" />
<RadioButton fx:id="monthlyMemberType" layoutX="412.0" layoutY="397.0" mnemonicParsing="false" text="Monthly" />
<RadioButton fx:id="yearlyMemberType" layoutX="499.0" layoutY="397.0" mnemonicParsing="false" text="Yearly" />
<TextField fx:id="discountField" editable="false" layoutX="246.0" layoutY="439.0" prefHeight="25.0" prefWidth="486.0" />
<Button fx:id="saveMemberButton" layoutX="246.0" layoutY="482.0" mnemonicParsing="false" onAction="#saveButtonClick" prefHeight="30.0" prefWidth="164.0" text="Save" />
<Button fx:id="cancelMemberButton" layoutX="499.0" layoutY="482.0" mnemonicParsing="false" onAction="#cancelButtonClick" prefHeight="30.0" prefWidth="164.0" text="Cancel" />
<ToggleButtonGroup layoutX="130.0" layoutY="170.0" selectionType="SINGLE" />
</children>
</Pane>
This is the error I get when run my main:
javafx.fxml.LoadException:
/C:/Users/ADMIN/eclipse-workspace/Project_V2_V2/bin/view/AddCustomer.fxml
at javafx.fxml#19/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml#19/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2949)
at javafx.fxml#19/javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2793)
at javafx.fxml#19/javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2758)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2624)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml#19/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at Project_V2_V2/application.Main.start(Main.java:22)
at javafx.graphics#19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics#19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics#19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics#19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics#19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics#19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.ClassNotFoundException: com.gluonhq.charm.glisten.control.ToggleButtonGroup
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:3017)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:3006)
at javafx.fxml#19/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2947)
... 20 more
I searched online for problems, i found all of the issues that has been offered online are not similar to mine at least from what i understood. I fixed some things that were in my code. However, it still did not solve the issue for me. Please i need help fixing this issue.

You are using Gluons com.gluonhq.charm.glisten.control.ToggleButtonGroup from their mobile tooling. Do you really want to do that? Maybe using the standard JavaFX ToggleGroup would be sufficient. If you really want to use Gluons lib then you have to add this implementation 'com.gluonhq:charm-glisten:6.2.2' to your dependencies. But note, that glisten is a proprietary offering with a different license than JavaFX.

Related

JavaFX - Adding Data to a TableView from a Different Scene

I am attempting to capture the TextField and Control Field values entered from the addItems.java class. Then display that data on the TableView generated on the shoppingCartController.java class. I do not get an error message when I hit add on the addItems.java file, but when I go back to the main shoppingCartController scene the entered data is not there.
Below is what is called when the 'Add' button is selected:
public void handleitemAdd(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("additems.fxml"));
Parent addItem_page = loader.load();
ObservableList<Products> list = FXCollections.observableArrayList();
list.add(new Products(productPriority.toString(),
productName.getText(),
Double.parseDouble(productPrice.getText()),
Integer.parseInt(productQty.getText())));
table.getItems().add(list);
System.out.println("Displaying information to consoles: Ensuring the addItem method worked as expected.");
}
Any assistance in reviewing this issue would be appreciated.
addItems.java
package shoppingcart;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class addItems implements Initializable {
// Fields used to add items to cart
#FXML private TextField productName = new TextField();
#FXML private TextField productQty = new TextField();
#FXML private TextField productPrice = new TextField();
#FXML private ChoiceBox productPriority = new ChoiceBox();
// Create the TableView
TableView table = new TableView(shoppingCartController.getProduct());
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//Used to Initialize the Scene
}
public void handleitemAdd(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("additems.fxml"));
Parent addItem_page = loader.load();
ObservableList<Products> list = FXCollections.observableArrayList();
list.add(new Products(productPriority.toString(),
productName.getText(),
Double.parseDouble(productPrice.getText()),
Integer.parseInt(productQty.getText())));
table.getItems().add(list);
System.out.println("Displaying information to consoles: Ensuring the addItem method worked as expected.");
}
public void handleitemReturnCart(ActionEvent event) throws IOException {
Parent shoppingCart_page = FXMLLoader.load(getClass().getResource("shoppingcart.fxml"));
Scene shoppingCart_scene = new Scene(shoppingCart_page);
Stage shoppingCart_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
shoppingCart_stage.setScene(shoppingCart_scene);
shoppingCart_stage.show();
System.out.println("Displaying information to console: Ensuring that user returned to main page");
}
}
addItems.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="" fx:id="productPage" prefHeight="750.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="shoppingcart.addItems">
<children>
<GridPane layoutX="189.0" layoutY="193.0" prefHeight="364.0" prefWidth="399.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="83.0" minHeight="10.0" prefHeight="66.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="88.0" minHeight="10.0" prefHeight="88.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="76.0" minHeight="10.0" prefHeight="59.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="99.0" minHeight="10.0" prefHeight="70.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text id="" fx:id="labelitemPriority" strokeType="OUTSIDE" strokeWidth="0.0" text="Item Priority:" />
<ChoiceBox id="" fx:id="productPriority" disable="true" prefWidth="200.0" GridPane.columnIndex="1" />
<TextField id="" fx:id="productName" prefHeight="14.0" prefWidth="63.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text id="" fx:id="labelitemName" strokeType="OUTSIDE" strokeWidth="0.0" text="Item Name:" GridPane.rowIndex="1" />
<TextField id="" fx:id="productQty" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Text id="" fx:id="labelitemQty" strokeType="OUTSIDE" strokeWidth="0.0" text="Item Qty:" GridPane.rowIndex="2" />
<Text id="" fx:id="labelitemPrice" strokeType="OUTSIDE" strokeWidth="0.0" text="Item Price:" GridPane.rowIndex="3" />
<TextField id="" fx:id="productPrice" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Button id="itemsAdd" fx:id="productAdd" onAction="#handleitemAdd" prefHeight="37.0" prefWidth="210.0" text="ADD" GridPane.columnIndex="1" GridPane.rowIndex="4" />
</children>
</GridPane>
<Text layoutX="354.0" layoutY="146.0" scaleX="4.085561690303489" scaleY="4.947136563876652" strokeType="OUTSIDE" strokeWidth="0.0" text="Add Items" wrappingWidth="67.541015625">
<font>
<Font size="14.0" />
</font></Text>
<Button id="returnCartHome" fx:id="productHome" layoutX="288.0" layoutY="609.0" mnemonicParsing="false" onAction="#handleitemReturnCart" prefHeight="48.0" prefWidth="201.0" text="Return to Cart" />
</children>
</AnchorPane>
shoppingCartController.java
package shoppingcart;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class shoppingCartController implements Initializable {
//Table used for Shopping Cart
#FXML private TableView<Products> item_Table;
#FXML private TableColumn<Products, String> item_Priority;
#FXML private TableColumn<Products, String> item_Name;
#FXML private TableColumn<Products, Number> item_Qty;
#FXML private TableColumn<Products, Number> item_Price;
private ObservableList<Products> productItems;
//The Initializer used to load data prior to loading view.
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
item_Priority.setCellValueFactory(cellData -> cellData.getValue().itemPriorityProperty());
item_Name.setCellValueFactory(cellData -> cellData.getValue().itemNameProperty());
item_Qty.setCellValueFactory(cellData -> cellData.getValue().itemQtyProperty());
item_Price.setCellValueFactory(cellData -> cellData.getValue().itemPriceProperty());
//Display all items in table
item_Table.setItems(getProduct());
}
// Method used to get the list of products
public static ObservableList<Products> getProduct() {
//Obseravable list which can be used to collect items
ObservableList<Products> products = FXCollections.observableArrayList();
return products;
}
public void handleitemAddition(ActionEvent event) throws IOException {
Parent addItem_page = FXMLLoader.load(getClass().getResource("addItems.fxml"));
Scene addItem_scene = new Scene(addItem_page);
Stage addItem_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
addItem_stage.setScene(addItem_scene);
addItem_stage.show();
System.out.println("Displaying information to consoles: Deleting Selected Item");
}
public void handleitemDelete(ActionEvent event) throws IOException {
System.out.println("Displaying information to consoles: Deleting Selected Item");
}
}
shoppingcart.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="750.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="shoppingcart.shoppingCartController">
<TableView fx:id="item_Table" layoutX="77.0" layoutY="174.0" prefHeight="352.0" prefWidth="646.0" tableMenuButtonVisible="true">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<columns>
<TableColumn fx:id="item_Priority" editable="false" prefWidth="75.0" text="Priority">
<cellValueFactory>
<PropertyValueFactory property="itemPriority" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="item_Name" editable="false" prefWidth="75.0" text="Item Name">
<cellValueFactory>
<PropertyValueFactory property="itemName" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="item_Qty" editable="false" prefWidth="75.0" text="Item Qty">
<cellValueFactory>
<PropertyValueFactory property="itemQty" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="item_Price" editable="false" prefWidth="75.0" text="Item Price">
<cellValueFactory>
<PropertyValueFactory property="itemPrice" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
<Text layoutX="199.0" layoutY="119.0" scaleX="1.4035087719298245" scaleY="1.7005740221599253" strokeType="OUTSIDE" strokeWidth="0.0" text="Shopping Cart Application" wrappingWidth="401.1374694108964">
<font>
<Font size="34.0" />
</font>
</Text>
<Button id="itemsDelete" layoutX="501.0" layoutY="571.0" mnemonicParsing="false" onAction="#handleitemDelete" prefHeight="46.0" prefWidth="222.0" text="Delete Selected" />
<Button fx:id="itemsAdd" layoutX="77.0" layoutY="571.0" onAction="#handleitemAddition" prefHeight="46.0" prefWidth="222.0" text="Add Items" />
</AnchorPane>
Edit:
I appreciate the assistance, I am still learning Java and had a follow-up question.
I updated the addItems.java to include:
public void handleitemAdd(ActionEvent event) throws IOException {
products.add(new Products(productPriority.toString(),
productName.getText(),
Double.parseDouble(productPrice.getText()),
Integer.parseInt(productQty.getText())));
System.out.println("Displaying information to consoles: Ensuring the addItem method worked as expected.");
}
As well as the shoppingCartController to include:
public void setTableItems(ObservableList<Products> products) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("shoppingcart.fxml"));
Scene shoppingCart_scene = new Scene(loader.load());
shoppingCartController controller = loader.getController();
controller.setTableItems(products);
}
But the value is still not carried over, I also ensured that the parameters are being captured (confirmed with debug mode).
As #Kleopatra said in the comment static access can be tricky sometimes so be careful and stick to java common naming conventions your code is hard to trace
you can pass the item back to the ShoppingCart when returning to it by setting the list via the controller
FXMLLoader loader = new FXMLLoader(getClass().getResource("shoppingcart.fxml"));
Scene shoppingCart_scene = new Scene(loader.load());
shoppingCartController controller = loader.getController();
controller.setProducts(list);
and in setProduct() function you can update the table
OR
You can create your ObservableList outside the static function but I don't recommend so and I suggest you search about passing data between controllers in javafx see the link below
https://stackoverflow.com/a/14190310/5303683

JavaFX control spinner dont works

Hi I'd like add to control "Spinner" integers between 1 to 24. I write my function and i added to fxml file but display exception (JavaFX 11).
Normally while I build window display spinner with default value "7" i can also change int from 1 to 24 but with each click display this exception.
Summarizing,
addChangeVIEW.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="305.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.controller.addChangeController">
<children>
<Button fx:id="addChangeButton" graphicTextGap="8.0" layoutX="413.0" layoutY="110.0" mnemonicParsing="false" prefHeight="85.0" prefWidth="128.0" text="ADD CHANGE" textAlignment="CENTER" textFill="#e50a0a" textOverrun="LEADING_WORD_ELLIPSIS" />
<DatePicker fx:id="dateChangePicker" layoutX="23.0" layoutY="138.0" />
<Label fx:id="descDatePicker" layoutX="70.0" layoutY="100.0" prefHeight="18.0" prefWidth="89.0" text="Date Change">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="fromHourLabel" layoutX="215.0" layoutY="99.0" text="From Hour" />
<Label fx:id="toHourLabel" layoutX="311.0" layoutY="99.0" text="To Hour" />
<Label fx:id="statusLabel" layoutX="83.0" layoutY="216.0" prefHeight="33.0" prefWidth="258.0" text="WAIT FOR ADD CHANGE" textFill="#14f004">
<font>
<Font name="Arial" size="18.0" />
</font>
</Label>
<Spinner fx:id="fromHourSpinner" layoutX="214.0" layoutY="137.0" onMouseClicked="#spinnersAction" prefHeight="26.0" prefWidth="58.0" />
<Spinner fx:id="toHourSpinner" accessibleText="1" layoutX="303.0" layoutY="137.0" onMouseClicked="#spinnersAction" prefHeight="26.0" prefWidth="58.0" />
</children>
</AnchorPane>
addChangeController
package com.controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import java.net.URL;
import java.util.ResourceBundle;
public class addChangeController implements Initializable {
#FXML
private Spinner<Integer> fromHourSpinner;
#FXML
private Spinner<Integer> toHourSpinner;
SpinnerValueFactory<Integer> spinner = new SpinnerValueFactory.IntegerSpinnerValueFactory(1,24,7);
#FXML
public void spinnersAction(ActionEvent event){
//fromHourSpinner.setValueFactory(spinner);
}
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
fromHourSpinner.setValueFactory(spinner);
}
}
Init
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
Process finished with exit code 0
As i have same problem, So here is the solution i found
Just need to rearrange the code a little bit like this..
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
SpinnerValueFactory<Integer> spinner =new SpinnerValueFactory.IntegerSpinnerValueFactory(1,24,7);
fromHourSpinner.setValueFactory(spinner);
toHourSpinner.setValueFactory(spinner);
}

Couldn't add listener to text field javafx

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);}
}

JavaFX MVC what should be handled by the controller and what by the main application?

I've recently made a java swing application to play oxo and I'm trying to make it again in javaFX using the MVC design pattern. I'm strugling as to what should be handled in which part of my application.
How it looked in swing (see picture link)
The design in javaFX using fxml (see picture link)
The folder structure is (see picture link)
To make this abstract question more concrete: I handle the click in my controller but what do I do with it then? Do I handle my game logic there? Or should I just determine the source and send it up to my main app to do the rest?
http://imgur.com/a/z3dGL
Main:
package be.vincent_nagy.oxo;
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{
FXMLLoader loader = new FXMLLoader(getClass().getResource("oxo.fxml"));
Parent root = loader.load();
Scene mainScene = new Scene(root,600,600);
primaryStage.setTitle("OXO Spel");
primaryStage.setScene(mainScene);
primaryStage.show();
//Hier geef ik de controller toegang tot de main app.
OxoController controller = loader.getController();
controller.setMain(this);
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package be.vincent_nagy.oxo;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
public class OxoController {
#FXML
private GridPane mainGrid;
#FXML
private Button button5;
#FXML
private Button button3;
#FXML
private Button button2;
#FXML
private Button button4;
#FXML
private Button button6;
#FXML
private Button button7;
#FXML
private Button button8;
#FXML
private Button button9;
#FXML
private Button button1;
//Referentie naar de main applicatie
private Main main;
//Constructor wordt opgeroepen voor de initialize() methode
public OxoController(){
}
// tot de #FXML properties van deze controller
#FXML
private void initialize(){
}
//Referentie naar de main applicatie. Aanroeping vanuit main
public void setMain(Main main){
this.main = main;
}
#FXML
void handleButtonAction(ActionEvent event) {
if(event.getSource() instanceof Button){
Button src = (Button) event.getSource();
//send the src up to main or handle the game code here?
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane fx:id="mainGrid" alignment="center" gridLinesVisible="true" minHeight="600.0" minWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="be.vincent_nagy.oxo.OxoController">
<rowConstraints>
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
</rowConstraints>
<columnConstraints>
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
</columnConstraints>
<children>
<Button fx:id="button5" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button fx:id="button3" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" />
<Button fx:id="button2" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" />
<Button fx:id="button4" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1" />
<Button fx:id="button6" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Button fx:id="button7" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2" />
<Button fx:id="button8" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button fx:id="button9" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<Button fx:id="button1" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" />
</children>
</GridPane>
https://pastebin.com/dgfQJiEK

JAVAFX javafx.fxml.LoadException simple login page

Getting LoadExeption evertime i try to run this code.
the code ran fine the first time but after that i just keep getting this error.
Its just a simple login page with not much coding what so ever.
Main class below
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root=FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args); // tried changing this to string[0] didnt work
}
}
Main controller class below i wanted to add more sql stuff to this but i keep getting the same error.
In this i am just trying to change
package application;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import javafx.fxml.FXML;
public class MainController {
#FXML
private Label LblStatus;
#FXML
private TextField TxtUsername;
#FXML
private TextField TxtPassword;
public void Login(ActionEvent event)
{
LblStatus.setText("Login sucsess");
}
}
login page
All of this was created using scene builder and youtube.
so i basically have no idea why i am getting this error.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.MainController">
<!-- TODO Add Nodes -->
<children>
<Pane prefHeight="400.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TitledPane animated="false" layoutX="0.0" layoutY="0.0" prefHeight="400.0000999999975" prefWidth="500.0" text="LOGIN FORM" textAlignment="CENTER" wrapText="false">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextField fx:id="TxtUsername" layoutX="227.0" layoutY="129.0" prefWidth="200.0" promptText="Enter Username" />
<Label layoutX="129.0" layoutY="129.0" prefWidth="98.0" text="USERNAME:">
<font>
<Font size="15.0" fx:id="x1" />
</font>
</Label>
<Label font="$x1" layoutX="129.0" layoutY="183.0" prefWidth="98.0" text="PASSWORD:" />
<Button font="$x1" layoutX="224.0" layoutY="225.0" mnemonicParsing="false" onAction="#Login" text="LOGIN" />
<PasswordField fx:id="TxtPassword" layoutX="224.0" layoutY="183.0" prefWidth="200.0" promptText="Enter Password" />
<Label id="Status" fx:id="LblStatus" layoutX="259.0" layoutY="69.0" text="Status">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
<font>
<Font name="Arial Black" size="15.0" />
</font>
</TitledPane>
</children>
</Pane>
</children>
</AnchorPane>
You have the wrong imports in your controller. Replace
import java.awt.Label;
with
import javafx.scene.control.Label ;
etc...

Categories