Reputation: 489
I need to visualize some data very compact. Due to limited hight of each data container, I decided to move the heading of each container to the side and rotate it vertically. When rotating the label, it sticks to its parent's dimensions. The maximum length of the label is therefore limited by the width of the parent. How can I accomplish that the label's maxWidth is the actual maxHeight of the parent pane?
For each container, I use a GridPane. The label is inside a StackPane to set a border or to change the background color.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Test extends Application {
public void start(Stage primaryStage) throws Exception {
// Part of the code
GridPane gridPane = new GridPane();
StackPane namePane;
Label nameLabel;
// ...
// Header
gridPane.getColumnConstraints().add(new ColumnConstraints(40.0));
// Index
gridPane.getColumnConstraints().add(new ColumnConstraints(50.0));
// Name
gridPane.getColumnConstraints().add(new ColumnConstraints(100.0,150.0,400));
// int rows = ...; // Any integer between 1 and 6
int rows = 5;
for(int i = 0; i < rows; i++) {
gridPane.getRowConstraints().add(new RowConstraints(30));
}
namePane = new StackPane();
nameLabel = new Label("Name-123456789");
nameLabel.setStyle("-fx-rotate: -90;");
namePane.getChildren().add(nameLabel);
gridPane.add(namePane,0,0,1,rows);
// ...
// Debug only
gridPane.setGridLinesVisible(true);
// just for running the example
Scene scene = new Scene(gridPane,700,700);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Below two images representing how I would expect the label to look and how it actually looks.
I've already tried to change the maxWidth of the Lable to Double.MAX_VALUE without success.
nameLabel.setMaxWidth(Double.MAX_VALUE);
Upvotes: 1
Views: 486
Reputation: 82461
StackPane
treats the Label
as if the position was not modified by transforms. This also affects the computed sizes of the StackPane
.
To fix this you could wrap the Label
in a Parent
that does consider transformations when calculating it's size: Group
namePane.getChildren().add(new Group(nameLabel));
Note: This does not resize the Label
, if the height of namePane
becomes too small to contain it. To achieve that effect, you'd need to implement your own layout.
Upvotes: 3