I had a question earlier regarding how I can add panesfrom to a VBox in JavaFX FXML. My question now is how can I edit the things inside the pane I Added? the code for FXML files is still the same. the new edited Java Code is as Follows:
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
public class HotelReservationController implements Initializable{
@FXML
private ScrollPane scrollPaneContent;
@FXML
private VBox vboxData;
@FXML
private AnchorPane cardAnchor;
@FXML
private HBox cardHBox;
@FXML
private Text cardTitle;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
URL cardURL = getClass().getResource("/application/Cards.fxml");
// vboxData.getChildren().add(cardAnchor);
for (int j = 0; j < 10; j++) {
Parent cardAnchor=null;
Parent cardHBox=null;
Parent cardTitle=null;
try {
cardAnchor = FXMLLoader.load(cardURL);
cardHBox= FXMLLoader.load(cardURL);
cardTitle = FXMLLoader.load(cardURL);
cardTitle.setText("test" + j);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
vboxData.getChildren().add(cardAnchor);
}
try {
connectToHotel();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
However, when i did this i had the following error:
javafx.fxml.LoadException:
/C:/Users///////bin/application/HotelReservation.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.text.Text
at application.HotelReservationController.initialize(HotelReservationController.java:55)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
How can I add this text field and other text fields (and I guess handling the image pane and the button would be similar to handling them)?
So after looking into this, taking into consideration @James_D answer, all I had to do is make a controller for the Cards.fxml, and in that controller I should add the data.
mine was
public class CardsController implements Initializable{
@FXML
private Text cardTitle;
@FXML
private Text cardLocation;
@FXML
private Text cardRating;
@FXML
private Text cardDescription;
@FXML
private Button cardDetails;
@FXML
private ImageView cardPhoto;
@FXML
private HBox cardHBox;
@FXML
private AnchorPane cardAnchor;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
cardTitle.setText("Test Title");
cardLocation.setText(" Test Location" );
cardRating.setText("5");
cardDescription.setText("This is only for testing, data should be imported later");
}
}
Now other than the controller, in the HotelReservationController
class, specifically inside the for loop that i had it display all the cards, I should have done the following :
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Cards.fxml"));
loader.load();
my issue was with loader.load(). the initialize
method doesn't get called unless I load the loader, and this is the thing that caused me many issues.
Hopefully this will help other clueless people like me in the future.