To create a visually appealing layout with two labels and a region in between, consider placing the labels in an HBox. Adjust the hgrow
property for the labels and region to control their resizing behavior. You can enhance the design by using a linear gradient as the background for the region, creating a striking visual effect.
Illustrative Example
// Define a 20px wide horizontal gradient alternating between black and transparent colors
private static final Paint FILL = new LinearGradient(
0, 0,
10, 0,
false,
CycleMethod.REPEAT,
new Stop(0, Color.BLACK),
new Stop(0.5, Color.BLACK),
new Stop(0.5, Color.TRANSPARENT)
);
// Create a background style for regions
private static final Background BACKGROUND = new Background(new BackgroundFill(FILL, CornerRadii.EMPTY, Insets.EMPTY));
private static void addRow(Pane parent, String label1Text, String label2Text) {
// Instantiate labels
Label label1 = new Label(label1Text);
Label label2 = new Label('[' + label2Text + ']');
// Create filler region with "stroke width" of 2
Region filler = new Region();
filler.setPrefHeight(2);
filler.setBackground(BACKGROUND);
HBox hbox = new HBox(5, label1, filler, label2);
hbox.setAlignment(Pos.CENTER);
HBox.setHgrow(label1, Priority.NEVER);
HBox.setHgrow(label2, Priority.NEVER);
HBox.setHgrow(filler, Priority.ALWAYS);
hbox.setFillHeight(false);
parent.getChildren().add(hbox);
}
@Override
public void start(Stage primaryStage) {
VBox root = new VBox();
addRow(root, "JBoss", "DOWN");
addRow(root, "GlassFish", "UP");
addRow(root, "verylongprocessname", "UP");
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
An alternative approach could involve applying a border (top only) to the region instead of setting a background. Omitting the prefHeight
allows for noticeable dots rather than strokes. However, based on the provided context, the background method is recommended for maintaining consistency with the desired visual appearance.