Second (and subsequent) calls to setBorder on a subclass of Region does not fully update the Border property. My test is using a single BorderStroke. It seems that the Paint, Widths, Radii, etc get updated but not the Insets.
This program demonstrates the issue:
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application
{
class SubRegion extends Region
{
private final Rectangle rect = new Rectangle(10,10, Color.BLACK);
SubRegion() {
getChildren().add(rect);
}
@Override
protected void layoutChildren() {
Insets in = getInsets();
System.out.println(in);
rect.setX(in.getLeft());
rect.setY(in.getTop());
rect.setWidth(getWidth() - in.getLeft() - in.getRight());
rect.setHeight(getHeight() - in.getTop() - in.getBottom());
}
}
@Override
public void start(Stage stage) throws Exception
{
final BorderWidths borderWidth2 = new BorderWidths(2);
final Insets firstInsets = new Insets(5, 5, 100, 100); // TRBL
final Insets secondInsets = new Insets(120, 120, 3, 3); // TRBL
final Border orange_border = new Border(new BorderStroke(Color.ORANGE, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, borderWidth2, firstInsets));
final Border green_border = new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, borderWidth2, secondInsets));
SubRegion sr = new SubRegion();
//
// Uncomment lines for these tests:
//
// Test Name | Lines UNcommented | Result
// -----------------|-------------------|-----------------------------------------------
// orange only | 1 | orange border around black box in upper right
// green only | 3 | green border around black box in lower left
// demonstrate bug | 1 and 3 | green border in lower left, black box in the upper right (at the former Insets)
// workaround | 1, 2 and 3 | same as "green only" ie. correct
//
sr.setBorder(orange_border); // line "1"
//sr.setBorder(null); // line "2"
sr.setBorder(green_border); // line "3"
stage.setTitle("Hello World");
stage.setScene(new Scene(sr, 300, 275));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This program demonstrates the issue:
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application
{
class SubRegion extends Region
{
private final Rectangle rect = new Rectangle(10,10, Color.BLACK);
SubRegion() {
getChildren().add(rect);
}
@Override
protected void layoutChildren() {
Insets in = getInsets();
System.out.println(in);
rect.setX(in.getLeft());
rect.setY(in.getTop());
rect.setWidth(getWidth() - in.getLeft() - in.getRight());
rect.setHeight(getHeight() - in.getTop() - in.getBottom());
}
}
@Override
public void start(Stage stage) throws Exception
{
final BorderWidths borderWidth2 = new BorderWidths(2);
final Insets firstInsets = new Insets(5, 5, 100, 100); // TRBL
final Insets secondInsets = new Insets(120, 120, 3, 3); // TRBL
final Border orange_border = new Border(new BorderStroke(Color.ORANGE, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, borderWidth2, firstInsets));
final Border green_border = new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, borderWidth2, secondInsets));
SubRegion sr = new SubRegion();
//
// Uncomment lines for these tests:
//
// Test Name | Lines UNcommented | Result
// -----------------|-------------------|-----------------------------------------------
// orange only | 1 | orange border around black box in upper right
// green only | 3 | green border around black box in lower left
// demonstrate bug | 1 and 3 | green border in lower left, black box in the upper right (at the former Insets)
// workaround | 1, 2 and 3 | same as "green only" ie. correct
//
sr.setBorder(orange_border); // line "1"
//sr.setBorder(null); // line "2"
sr.setBorder(green_border); // line "3"
stage.setTitle("Hello World");
stage.setScene(new Scene(sr, 300, 275));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}