Execute the test below - the listener should be called 30 times but for some reason the number of calls varies from 1 to 30, mostly it's 5-15. The test works fine on Java 7u45 and Java 8b81. I don't know exactly which release introduced the issue - it does not work with Java 8b99. The issue also does not occur when you remark the line fxmlLoader.load().
public class PanelSizePropertyTest extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
final StackPane root = new StackPane();
Button b = new Button("Add Panes");
b.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
for (int i=0; i<30; i++)
{
MyPane myPane = new MyPane(root, i);
root.getChildren().add(myPane);
}
}
});
root.getChildren().add(b);
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
stage.show();
}
private static class MyPane extends Pane
{
public MyPane(Pane parent, final int i)
{
try
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/test/bind/test.fxml"));
//when the line below is remarked the issue does not occur
fxmlLoader.load();
}
catch (Exception e)
{
e.printStackTrace();
}
final DoubleProperty x = new SimpleDoubleProperty();
x.bind(widthProperty());
x.addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> obs, Number oldValue, Number newValue)
{
System.err.println(" x" + i + " " + newValue);
}
});
getChildren().add(new Button("Test"));
setMouseTransparent(true);
}
}
}
test.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<GridPane id="rootPaneTitlePane" alignment="TOP_LEFT" gridLinesVisible="false" hgap="0.0" layoutY="0.0" minHeight="-1.0" prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml">
<children>
<Label fx:id="titleLabel" alignment="CENTER" contentDisplay="LEFT" prefWidth="-1.0" text="title" textAlignment="CENTER" wrapText="false" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.hgrow="NEVER" GridPane.rowIndex="0">
<GridPane.margin>
<Insets fx:id="x1" />
</GridPane.margin>
</Label>
<Button fx:id="closeButton" graphicTextGap="0.0" maxWidth="-1.0" minHeight="-1.0" minWidth="-1.0" mnemonicParsing="false" prefHeight="-1.0" prefWidth="-1.0" text="" GridPane.columnIndex="1" GridPane.margin="$x1" GridPane.rowIndex="0" />
</children>
<columnConstraints>
<ColumnConstraints fillWidth="true" hgrow="ALWAYS" maxWidth="-Infinity" minWidth="-1.0" prefWidth="-1.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="-1.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="-Infinity" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
public class PanelSizePropertyTest extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
final StackPane root = new StackPane();
Button b = new Button("Add Panes");
b.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
for (int i=0; i<30; i++)
{
MyPane myPane = new MyPane(root, i);
root.getChildren().add(myPane);
}
}
});
root.getChildren().add(b);
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
stage.show();
}
private static class MyPane extends Pane
{
public MyPane(Pane parent, final int i)
{
try
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/test/bind/test.fxml"));
//when the line below is remarked the issue does not occur
fxmlLoader.load();
}
catch (Exception e)
{
e.printStackTrace();
}
final DoubleProperty x = new SimpleDoubleProperty();
x.bind(widthProperty());
x.addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> obs, Number oldValue, Number newValue)
{
System.err.println(" x" + i + " " + newValue);
}
});
getChildren().add(new Button("Test"));
setMouseTransparent(true);
}
}
}
test.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<GridPane id="rootPaneTitlePane" alignment="TOP_LEFT" gridLinesVisible="false" hgap="0.0" layoutY="0.0" minHeight="-1.0" prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml">
<children>
<Label fx:id="titleLabel" alignment="CENTER" contentDisplay="LEFT" prefWidth="-1.0" text="title" textAlignment="CENTER" wrapText="false" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.hgrow="NEVER" GridPane.rowIndex="0">
<GridPane.margin>
<Insets fx:id="x1" />
</GridPane.margin>
</Label>
<Button fx:id="closeButton" graphicTextGap="0.0" maxWidth="-1.0" minHeight="-1.0" minWidth="-1.0" mnemonicParsing="false" prefHeight="-1.0" prefWidth="-1.0" text="" GridPane.columnIndex="1" GridPane.margin="$x1" GridPane.rowIndex="0" />
</children>
<columnConstraints>
<ColumnConstraints fillWidth="true" hgrow="ALWAYS" maxWidth="-Infinity" minWidth="-1.0" prefWidth="-1.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="-1.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="-Infinity" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>