2

I'm creating a LinkedList which contains favorite movies of someone.

I want to display the whole list in FXML, but a Label doesn't support multi-line output.

This is the code I tried (filmFavoriten is the LinkedList):

static LinkedList<Film> filmFavoriten = new LinkedList<>();

@FXML
private void displayList() throws IOException {
    GridPane pane = FXMLLoader.load(getClass().getClassLoader().getResource("displayList.fxml"));
    rootpane.getChildren().setAll(pane);

    for (int r = 0; r < filmFavoriten.size(); r++) {
        Label listTable = new Label();
        listTable.setText((r + 1) + ". " + filmFavoriten.get(r).title);
    }
}

Please suggest.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Japhet
  • 161
  • 1
  • 2
  • 12
  • While I don't see a `LinkedList` I assume it's somewhere (and your `filmFavoriten` is of that type). You are calling `get(index)` on it, which is something you should not be doing for a `LinkedList`. Use an `ArrayList` if you want random access. – Ben Jun 14 '18 at 08:16
  • the LinkedList is created at the start of the class. I added it to the code above for clarification. – Japhet Jun 14 '18 at 08:16
  • @Ben, there is nothing wrong to use `get(i)` on any `List` implementation – Andrew Tobilko Jun 14 '18 at 08:31
  • @Andrew Yes, I guess my wording was off. Using `get` on a `LinkedList` is invoking a larger overhead than calling it on e.g. an `ArrayList` instead as the implementation of the `LinkedList` as you can not random access on a `LinkedList` but have to go through all elements to the correct index. So if you have the choice and want to access data with random access then it's preferable to not use a `LinkedList`. I hope that's clearer? – Ben Jun 14 '18 at 08:37
  • Although obviously there are ways around this issue, e.g. using the List's iterator which would be an easy solution here without changing the List implementation. – Ben Jun 14 '18 at 08:38
  • 1
    "_...`Label` doesn't support multi-line output_". Are you sure about that? I just tested a `Label` with text that contained a `\n` character and it displayed the text on two lines... – Slaw Jun 14 '18 at 12:38
  • 1
    @Slaw in my question I wrote "... muIti-line output I think (or I'm just not able to implement it)". It got edited out. – Japhet Jun 14 '18 at 14:34

1 Answers1

1

You are doing the right job, but newly created Labels in the loop won't be added to the GridPane automatically.

for (...) {
    ...
    pane.getChildren().add(listTable);
}

... but a Label doesn't support multiline output

According to your code, each film owns a label without any line breaks. The films are being printed into separate Labels, so there is no need of merging lines by a '\n'.

I would recommend using a ListView<Film> or Vbox (see an example below).

VBox verticalBox = new VBox();

for (...) {
    ...
    verticalBox.getChildren().add(listTable);
}

pane.getChildren().add(verticalBox);
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • thank you, I've done that and now it displays the list. However it's printing the List out on top of eachother and its ignoring the Labels that are already declared in the fxml file and its printing the list out on top of those labels too. Is there a way to add a new line for each list item? and make it not collide with the labels declared in the fxml file? – Japhet Jun 14 '18 at 09:01
  • @Japhet, next, we need a structure to organise the position of the labels. Updated the answer – Andrew Tobilko Jun 14 '18 at 09:09
  • thanks a lot, now it displays the whole list! on more question tho: Is there a way to make the VBox implemented in the controller not collide with the structures declared in the fxml file? Right now the VBox is printed out over my "headline" Label, which I declared in the fxml file. – Japhet Jun 14 '18 at 09:29
  • @Japhet, you could declare a `VBox` in the `fxml` file (making sure it doesn't collide with the existent structure) and fetch it by id rather than creating a new one. – Andrew Tobilko Jun 14 '18 at 09:44
  • If you have more problems, ask a new question. – SedJ601 Jun 14 '18 at 13:39
  • 1
    FYI: This is a good answer, but you should probably use a `TableView` or `ListView` instead of `VBox`. These controls with do a better job memory-wise(if implemented correctly) if you have to list a lot of entries. To control your layout(overlapping) better, you should do some research on all the `Parent` nodes. – SedJ601 Jun 14 '18 at 13:41
  • Also, I rarely see people use `LinkedList` anymore. You can check [here](https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist) to see if it's good in your case. – SedJ601 Jun 14 '18 at 13:50