-2

I'm still pretty new to this so I did quite a reseach but couldn't found any suitable solution on SO or on the interet. How can I create multiple instances of object (you can call it a factory) based on one .fxml file. This particular .fxml file represents one record of movie information. And the result looks similar to this (I won't post here the fxml code, because it's not relevant): enter image description here

So this is one record. As you can see I need sort of template. Let's say I got to display information about 5 movies, so I wish I could do :

    MovieView movieView = new MovieView(name, imageUrl, year, List<String> directors, 
    List<String> actors, Integer boxOffice, reviews);
    movieView.setImage(new Image(...));  // image change
    vbox.getChildren().add(movieView); // append to main panel 

five times and fill the VBox with five instances of the same class MovieView. How can this be done?

user7303261
  • 128
  • 12
  • 1
    Study the oracle tutorial: [4 Creating a Custom Control with FXML](https://docs.oracle.com/javase/8/javafx/fxml-tutorial/custom_control.htm). It tells you exactly what you need to do. Also see the section [custom components](https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#fxmlloader) or the fxml intro doc. The key is using `fx:root`. – jewelsea Nov 26 '21 at 00:36

1 Answers1

1

new keyword creates a new instance of an object in java.

Each call to new MovieView(....) will create a new instance of it. So if you need 5 instances, you would create it 5 times.

For example, you can do something like this to create 5 movie views and add them to a list

 List<MovieView> movieViews = new ArrayList<>();
 for (int i = 0; i < 5; i++) {
     // Creates a new instance on each iteration
     MovieView movieView = new MovieView(...);
     movieViews.add(movieView)
 }

Depending on your use case you'll have to pass in appropriate arguments and potentially create new instances of Image for each movie. Also, 5 could be any number here depending on how many instances you need. You can pass it as an argument to your method. This example should give you a starting point on how to implement your solution.

Naveed
  • 2,942
  • 2
  • 25
  • 58
  • 1
    Thank you for the answer, but it's pretty much useless. You missed my point. I need to know how to "connect" my `MoviewView.fxml` and MovieView class - you can call it controller. But I'm not sure if that's the way contollers work. – user7303261 Nov 25 '21 at 21:53
  • 1
    @user7303261, just to remind you: There is no answer "useless" in this platform. If someone is missing your point means, you have not explained your question/issue properly. Do read how to [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) before posting a question. – Sai Dandem Nov 26 '21 at 00:47
  • 1
    @user7303261 The only sense in which this answer is “useless” is in the sense that you can’t be bothered to take the time to understand it. – James_D Nov 26 '21 at 01:30
  • 2
    If this answer is combined with the info in the duplicate, I think it provides an exact solution to the question as I understood it. – jewelsea Nov 26 '21 at 02:18