I want to add a little icon to the TitledPane header. So I set an empty title and add an HBox containing a Label and a ImageView as graphics. Thus, the icon is displayed closer to the end of the text. I want it to always appear next to the right border of the TitledPane . How can i do this? I also tried using BorderPane and add the Label to the center and ImageView to the right, but BorderPane does not get the maximum size of the TitledPane. So I tried setting MaxWidth to Max-Value, but that didn't help
Does anyone know what to do?
** EDIT: ** The user element I created will be initialized in the method specified in stage.setOnShown.
public class CustomTitledPane extends TitledPane { private Image alert; private Image registered; private Image deleted; private ImageView img; public CustomTitledPane(String titleText, Node node) { super(titleText, node); setAnimated(true); setCollapsible(true); img = new ImageView(); img.setFitHeight(10d); img.setPreserveRatio(true); img.setSmooth(true); setGraphic(img); setContentDisplay(ContentDisplay.RIGHT); // apply css and force layout of nodes applyCss(); layout(); // title region Node titleRegion = lookup(".title"); // padding Insets padding = ((StackPane) titleRegion).getPadding(); // image width double graphicWidth = img.getLayoutBounds().getWidth(); // arrow double arrowWidth = titleRegion.lookup(".arrow-button") .getLayoutBounds().getWidth(); // text double labelWidth = titleRegion.lookup(".text").getLayoutBounds() .getWidth(); double nodesWidth = graphicWidth + padding.getLeft() + padding.getRight() + arrowWidth + labelWidth; System.out.println("w: " + graphicWidth + " " + arrowWidth + " " + labelWidth); graphicTextGapProperty().bind(widthProperty().subtract(nodesWidth)); try { alert = new Image(new FileInputStream("img/Alert.png")); registered = new Image(new FileInputStream("img/Registered.png")); deleted = new Image(new FileInputStream("img/Deleted.png")); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
And here is the CSS for the TitledPane:
.titled-pane { -fx-text-fill: #006FD8; } .titled-pane > .title { -fx-background-color: transparent; -fx-border-color: linear-gradient(to right, white 0%, grey 30%, grey 70%, white 100%) transparent transparent transparent; } .titled-pane:expanded > .title { -fx-border-color: grey transparent transparent transparent; -fx-background-color: linear-gradient(to bottom, #DCE7F5, white); } .titled-pane:expanded > *.content { -fx-border-width: 0; }
source share