luhara
luhara

Reputation: 25

JavaFx: lookup doesn't find the correct Node

I have two Classes Highscore and HighscoreEinzelspieler. Highscore is the super Class where some logic for an table like view happens:

public VBox getDisplayHighscore(ArrayList<Spieler> spielerListe) {

    VBox output = new VBox();
    output.setAlignment(Pos.CENTER);
    HBox headerRow = new HBox();

    Iterator<Spieler> iterator = spielerListe.iterator();
    for (int i = 0; iterator.hasNext(); i++) {
        Spieler tmpPlayer = iterator.next();

        HBox tmpRow = new HBox();
        tmpRow.setAlignment(Pos.CENTER);
        tmpRow.getStyleClass().add("tableRow");
        tmpRow.setId("tableRowId" + i); //um spaeter die neue Position oder den Gewinner zu markieren

        Label tmpName = new Label(tmpPlayer.getName());
        tmpName.getStyleClass().add("tableName");
        Label tmpVersuche = new Label("" + tmpPlayer.getVersuche());
        tmpVersuche.getStyleClass().add("tableVersuche");
        Label tmpTimer = new Label("" + tmpPlayer.getTimer());
        tmpTimer.getStyleClass().add("tableTimer");
        Label tmpPaare = new Label("" + tmpPlayer.getPaare());
        tmpPaare.getStyleClass().add("tablePaare");

        tmpRow.getChildren().addAll(tmpName, tmpVersuche, tmpTimer, tmpPaare);
        output.getChildren().addAll(tmpRow);
      }

    output.getStyleClass().add("table");
    return output;
}

Now I need this Function in my Subclass HinghscoreEinzelspieler (the part with the comments doesn't work):

public HighscoreEinzelspieler() throws IOException, NamenFehler, AlterFehler {
    // Liste auslesen
    ArrayList<Spieler> spielerListe = getHighscore();
    spielerListe.sort(new SpielerComparatorEinzel());
    playerDisplay = super.getDisplayHighscore(spielerListe);
    playerDisplay.getStyleClass().add("table");

    //Node highlight = Masterklasse.primaryScene.lookup("#tableRowId1");
    //System.out.println(highlight);
    //highlight.getStyleClass().remove("tableRow");
    //highlight.getStyleClass().add("tableRowWinner");

    // mittig einbinden
    Masterklasse.centerBox.getChildren().addAll(playerDisplay);

}

Now I want to find one line from the getDisplayHighscore I created. For that I tried to get the line with the ID from my static scene with the function lookup("#tableRowId"+i). But I always get a "none", I tried it in the super class with:

output.lookup("tableRowId1")

and it worked. So I think the static scene is the problem.

Thanks, Luka :)

Upvotes: 0

Views: 395

Answers (2)

luhara
luhara

Reputation: 25

Sorry for my late answer, I fixed it now. It was something with so code position, I have to call this method later. But I don't know why it's now working. I can't explain it.

Upvotes: 0

Dmytro Maslenko
Dmytro Maslenko

Reputation: 2297

I don't know what is Masterklasse.primaryScene but this should work:

playerDisplay.lookup("#tableRowId1");

If Masterklasse.primaryScene should point on your output you need to assign it in getDisplayHighscore() method:

Masterklasse.primaryScene = output;

Upvotes: 1

Related Questions