You need to do some basic math. Follow modifications to your code below:
1) In fxml file add properties to HBox:
<HBox alignment="CENTER_RIGHT" HBox.hgrow="ALWAYS">
The result is, when there is an extra space in title width (by resizing), this HBox will have a priority to expand and its children will always be aligned to the right (center).
2) Now we need to calculate the remaining title width this way
REMAINING_WIDTH = TITLE_TOTAL_WIDTH - TITLE_TEXT_WIDTH - ARROW_BUTTON_WIDTH - RIGHT_LEFT_PADDINGS
The calculated width will be our tool buttons pane's (i.e. HBox's) preferred width.
public Parent createContent() {
String titleText = "Node 1";
TitledPane t1 = new TitledPane(null, new Button("Button"));
t1.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
t1.setGraphic(loadMinitool(titleText));
double titleTextWidth = computeTextWidth(t1.getFont(), titleText, 0);
double arrowButtonWidth = 14; // I have given a static value here otherwise
// it must be calculated by
// t1.lookup(".arrow-button").getLayoutBounds().getWidth()
// after the primary stage has been shown. Namely after primaryStage.show(); line.
double paddings = 20; // right (10) and left (10) paddings defined in
// caspian.css for title of the pane. These values can also be obtained
// by lookup.
double total = titleTextWidth + arrowButtonWidth + paddings;
HBox toolButtons = (HBox) ((BorderPane) t1.getGraphic()).getRight();
toolButtons.prefWidthProperty().bind(t1.widthProperty().subtract(total));
TitledPane t2 = new TitledPane("Node 2", new Text("String"));
TitledPane t3 = new TitledPane("Node 3", new Rectangle(120, 50,
Color.RED));
Accordion accordion = new Accordion();
accordion.getPanes().add(t1);
accordion.getPanes().add(t2);
accordion.getPanes().add(t3);
accordion.setMinSize(100, 100);
accordion.setPrefSize(200, 400);
return accordion;
}
The computeTextWidth()
code is taken from com.sun.javafx.scene.control.skin.Utils
, and for reference only:
private double computeTextWidth(Font font, String text, double wrappingWidth) {
Text helper = new Text();
helper.setText(text);
helper.setFont(font);
// Note that the wrapping width needs to be set to zero before
// getting the text's real preferred width.
helper.setWrappingWidth(0);
double w = Math.min(helper.prefWidth(-1), wrappingWidth);
helper.setWrappingWidth((int) Math.ceil(w));
return Math.ceil(helper.getLayoutBounds().getWidth());
}
The other codes of yours remain the same.