-3

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)?

TestUser1
  • 13
  • 5
  • 1
    That code doesn't compile; `Parent` doesn't have a `setText()` method. You need to call a method on the controller, see https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – James_D Dec 17 '20 at 18:59
  • so from what i understood is that i need to have a controller for the Cards.fxml, in that controller i create a method to change the text there dynamically, and i just call that method. then i load the cardAnchor attribute and then it should automatically set it? – TestUser1 Dec 17 '20 at 19:29
  • What does "edit text / Image/Button" mean? What would editing a button mean? Who is doing the editing, is it the end user or something in your program? Can you provide an example of an edit and what it does for each of the things you wish to make editable? An example of a [user editable label](https://stackoverflow.com/questions/25572398/how-do-i-create-an-editable-label-in-javafx-2-2) is here, but I don't think that it what you are really asking. – jewelsea Dec 17 '20 at 20:29
  • So, i have a layout that looks like a card, and i want to dynamically add it to a VBox in a Scroll Pane. the card has an image view, and 4 texts : for title,location,rating,and description, and it also has a button that when pressed will lead to a new view. in the linked question i asked how to add the cards dynamically to the VBox (all the code is there) and it worked as @James_D said. but then when i tried to edit the text dynamically ( i want to pull data from a data base and then edit each field in the For loop, but till now i am only testing so i haven't connected it yet// didn't work) . – TestUser1 Dec 17 '20 at 20:38

1 Answers1

0

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.

TestUser1
  • 13
  • 5