JavaFX HBox arranging the elements correctly

I’m using a HBox object. And I add Text objects to it. I want the text objects appear directly behind each other.
But my result looks like following:

enter image description here

So, some objects are higher like the others. And there is no line break, although I have a “n” in my text objects.

This is my code:

while (true) {
                    int index = currentPage.indexOf(searchText);

                    if (index == -1) {
                        break;
                    }

                    // neuer ROTER Text für den Suchtext
                    Text redText = new Text();
                    redText.setFill(Paint.valueOf("red"));
                    redText.setText(currentPage.substring(index, index + searchText.length())); // TODO passt so?

                    // berechne Grenzen für Substring
                    int left = Math.max(0, currentPage.length() - (currentPage.length() - index) - LENGTH_OF_TEXT_SHOWN);
                    int right = Math.min(currentPage.length() - 1, index + LENGTH_OF_TEXT_SHOWN);

                    // Text links neben rotem Text
                    Text leftText = new Text("n" + "Seite " + (i + 1) + ":t..." + currentPage.substring(left, index));

                    // Text rechts neben rotem Text
                    Text rightText = new Text(currentPage.substring(index + searchText.length(), right) + "...");

                    //newLabelText.append("n" + "Seite " + (i + 1) + ":t..." + currentPage.substring(left, right) + "...");

                    // Texte dem Panel hinzufügen
                    hBoxResults.getChildren().addAll(leftText, redText, rightText, new Text("n"));

                    // cut old part off
                    int startIndex = index + searchText.length() + 1;
                    if (startIndex < currentPage.length()) {
                        currentPage = currentPage.substring(startIndex);
                    }
                }

Source: java

Leave a Reply