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
Related
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.
I'm creating a simple login GUI form using eclipse, JavaFX and scene builder. I've coded my program so that every time I click the login button, it would switch to another window but it doesn't work. i'm a beginner in java but we've been tasked to create a system in my class so I've only relied on youtube tutorials, any help would be appreciated!
this is my main code
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("Main.fxml"));
Scene scene = new Scene(root);
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);
}
}
my login form
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Login {
#FXML
private Button button;
#FXML
private PasswordField password;
#FXML
private TextField username;
#FXML
private Label wrongLogin;
#FXML
public void Login(ActionEvent event) {
Stage primaryStage = new Stage();
if (username.getText().equals("admin") && password.getText().equals("admin")) {
try {
Parent root = FXMLLoader.load(getClass().getResource("BookRooms.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
my fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="345.0" prefWidth="545.0" style="-fx-background-color: #fff2cc;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Login">
<children>
<AnchorPane layoutX="132.0" layoutY="62.0" prefHeight="221.0" prefWidth="279.0" style="-fx-background-color: #545454; -fx-border-radius: 10;">
<children>
<Label layoutX="34.0" layoutY="60.0" prefHeight="25.0" prefWidth="56.0" text="Username:" textFill="WHITE" />
<Label layoutX="34.0" layoutY="98.0" prefHeight="25.0" prefWidth="56.0" text="Password:" textFill="WHITE" />
<TextField layoutX="106.0" layoutY="60.0" promptText="Enter Username ID:" />
<TextField layoutX="106.0" layoutY="98.0" promptText="Enter Password" />
<Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" />
</children></AnchorPane>
<Label layoutX="225.0" layoutY="36.0" text="HOTEL DEL LUNA" />
</children>
</AnchorPane>
Your button in fxml can't call Login() in its controller class because there is no onAction attribute in fxml button tag .
So ,in <Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" /> add onAction="#Login" inside Button Tag:
<Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" onAction="#Login" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" />
And , naming a method in a class with exact the same name of that class is concidered a bad practice. Only constructors must match its name with class identifier
I have a vbox which contains a list of cards(Anchor panes). I am trying to make each card draggable so that I can change the order of the list whenever I want. I am using a scene builder.
However, I keep getting the following message after I drag and attempt to drop.
Java Messsge:class javafx.scene.layout.VBox cannot be cast to class javafx.scene.layout.AnchorPane(javafx.scene.layout.VBox and javafx.scene.layout.AnchorPane are in module javafx.graphics of loader 'app')
My code:
minimal code provided only contains two cards, one of which is being used only in my code.
Edit: I no longer get the error message however, I am not getting my desired result as the cards are not changing positions
mport javafx.beans.property.ObjectProperty;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TitledPane;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Controller
{
#FXML public AnchorPane card1, card2;
#FXML public AnchorPane current;
#FXML public GridPane gridPane;
#FXML public GridPane column1;
#FXML public AnchorPane col1;
#FXML public Button button1, button2;
#FXML public Button currentButton;
#FXML public VBox v1;
#FXML
public void detect() { //detect card
card1.setOnDragDetected((MouseEvent event) -> { //card1 is the card
Dragboard db = card1.startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
content.putString(card1.getId());
WritableImage snapshot = card1.snapshot(new SnapshotParameters(), null);
db.setDragView(snapshot);
db.setContent(content);
event.consume();
});
}
#FXML
public void accept() { //add to vbox
v1.addEventHandler(DragEvent.DRAG_OVER, (DragEvent event) -> { //v1 is the vbox
if (event.getGestureSource() != v1
&& event.getDragboard().hasString()) {
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
event.consume();
});
}
#FXML
public void drop() {
v1.addEventHandler(DragEvent.DRAG_DROPPED, (DragEvent event) -> {
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasString()) {
VBox parent = (VBox) card1.getParent();
//int targetIndex = parent.getChildren().indexOf(card1);
List<Node> nodes = new ArrayList<Node>(parent.getChildren());
parent.getChildren().clear();
parent.getChildren().addAll(nodes);
success = true;
}
event.setDropCompleted(success);
event.consume();
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="772.0" prefWidth="1295.0" style="-fx-background-color: #C5F5D4;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="KanbanBoard.Controller">
<children>
<AnchorPane layoutY="47.0" prefHeight="687.0" prefWidth="1295.0">
<children>
<AnchorPane fx:id="col1" layoutX="-1.0" prefHeight="724.0" prefWidth="216.0">
<children>
<GridPane fx:id="column1" layoutX="33.0" layoutY="15.0" onMouseDragOver="#accept" prefHeight="724.0" prefWidth="209.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="353.8666748046875" minHeight="10.0" prefHeight="103.20001831054685" vgrow="SOMETIMES" />
<RowConstraints maxHeight="621.5999816894531" minHeight="10.0" prefHeight="621.5999816894531" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<VBox fx:id="v1" onDragDropped="#drop" onDragOver="#accept" prefHeight="200.0" prefWidth="210.0" GridPane.rowIndex="1">
<children>
<AnchorPane fx:id="card1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#detect" onMouseDragged="#handleLabel" prefHeight="125.0" prefWidth="198.0" style="-fx-background-color: #E5EAE6;">
<children>
<TextField layoutX="5.0" layoutY="3.0" prefHeight="26.0" prefWidth="65.0" promptText="ID" />
<TextField layoutX="72.0" layoutY="3.0" prefHeight="26.0" prefWidth="124.0" promptText="Title" />
<TextArea layoutX="5.0" layoutY="32.0" prefHeight="50.0" prefWidth="190.0" promptText="Description" />
<TextField layoutX="5.0" layoutY="85.0" prefHeight="26.0" prefWidth="124.0" promptText="Story point" />
<Button fx:id="button1" layoutX="164.0" layoutY="85.0" mnemonicParsing="false" onMouseClicked="#removeCard" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="-" />
<Button layoutX="129.0" layoutY="85.0" mnemonicParsing="false" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="+" />
</children>
</AnchorPane>
<AnchorPane fx:id="card2" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#detect" onMouseClicked="#handleLabel" prefHeight="125.0" prefWidth="198.0" style="-fx-background-color: #E5EAE6;">
<children>
<TextField layoutX="5.0" layoutY="3.0" prefHeight="26.0" prefWidth="65.0" promptText="ID" />
<TextField layoutX="72.0" layoutY="3.0" prefHeight="26.0" prefWidth="124.0" promptText="Title" />
<TextArea layoutX="5.0" layoutY="32.0" prefHeight="50.0" prefWidth="190.0" promptText="Description" />
<TextField layoutX="5.0" layoutY="85.0" prefHeight="26.0" prefWidth="124.0" promptText="Story point" />
<Button fx:id="button2" layoutX="164.0" layoutY="85.0" mnemonicParsing="false" onMouseClicked="#removeCard" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="-" />
<Button layoutX="129.0" layoutY="85.0" mnemonicParsing="false" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="+" />
</children>
</AnchorPane>
</children>
</VBox>
<Pane prefHeight="98.0" prefWidth="226.0" style="-fx-background-color: #D1D3D1;">
<children>
<Button layoutX="124.0" layoutY="64.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="26.0" text="-" />
<TextArea layoutY="39.0" prefHeight="50.0" prefWidth="124.0" promptText="Role" />
<TextField layoutX="1.0" layoutY="5.0" prefHeight="26.0" prefWidth="124.0" promptText="Name" />
<Button fx:id="addColumnButton" layoutX="124.0" layoutY="36.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="26.0" text="+" />
</children>
</Pane>
</children>
</GridPane>
</children>
</AnchorPane>
<AnchorPane layoutX="1082.0" prefHeight="726.0" prefWidth="213.0">
<children>
<Pane prefHeight="50.0" prefWidth="214.0" style="-fx-background-color: #8F9691;">
<children>
<Label layoutX="49.0" layoutY="7.0" prefHeight="36.0" prefWidth="46.0" text="Log" textAlignment="CENTER" textOverrun="WORD_ELLIPSIS">
<font>
<Font size="22.0" />
</font>
</Label>
</children>
</Pane>
<Pane layoutY="49.0" prefHeight="676.0" prefWidth="214.0" style="-fx-background-color: #C6C8C6;" />
</children>
</AnchorPane>
<GridPane fx:id="gridPane" layoutX="228.0" onDragDropped="#drop" onDragOver="#accept" prefHeight="726.0" prefWidth="856.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="627.1999938964843" minHeight="10.0" prefHeight="622.4000061035156" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
<Label layoutX="381.0" layoutY="-4.0" prefHeight="50.0" prefWidth="180.0" text="Kanban Board">
<font>
<Font size="28.0" />
</font>
</Label>
</children>
</AnchorPane>
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.stage.Stage;
import java.io.IOException;
public class Kanban extends Application {
//private Button b;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("Kanban.fxml"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Controller controller = new Controller();
FXMLLoader loader = new FXMLLoader();
loader.setController(controller);
//Controller c = loader.getController();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Can someone help me out please.
AnchorPane parent = (AnchorPane) card1.getParent(); (In the drop() method, at the bottom of your code)
card1.getParent() with a type of VBox cannot be cast to AnchorPane
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
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...