(apologies in advance for using file names and resources that are hard coded, but this application is part of a larger application).
The following code works fine:
VBox v = new VBox();
Button b = new Button();
b.setGraphic(new ImageView(new Image(Resources.class.getResource("cnr.png").toString())));
b.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent evt)
{
System.err.println("Got a button action");
}
});
v.getChildren().addAll(b);
Scene sx = new Scene(v, 200, 200);
stage.setScene(sx);
stage.setVisible(true);
Specifically clicking the button once, including clicking on the graphic, causes the "Got a button action" message to appear.
The following code works differently however:
VBox v = new VBox();
Button b = new Button();
b.getStyleClass().add("run-button");
b.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent evt)
{
System.err.println("Got a button action");
}
});
v.getChildren().addAll(b);
Scene sx = new Scene(v, 200, 200);
sx.getStylesheets().add(Main.class.getResource("styleControl.css").toExternalForm());
stage.setScene(sx);
stage.setVisible(true);
where the styleControl.css file looks like this
.run-button
{
-fx-graphic: url("file:resources/cnr.png");
}
In this case clicking on the non-graphic part of the button causes the "Got a button action" message to appear, but clicking on the graphic part of the button does not (strangely two clicks on the graphic part does cause the message to appear).
It appears that the process of creating the graphic image when using the CSS style sheet is not the same as the default ImageView creation code. This makes CSS styling a lot less useful....
The following code works fine:
VBox v = new VBox();
Button b = new Button();
b.setGraphic(new ImageView(new Image(Resources.class.getResource("cnr.png").toString())));
b.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent evt)
{
System.err.println("Got a button action");
}
});
v.getChildren().addAll(b);
Scene sx = new Scene(v, 200, 200);
stage.setScene(sx);
stage.setVisible(true);
Specifically clicking the button once, including clicking on the graphic, causes the "Got a button action" message to appear.
The following code works differently however:
VBox v = new VBox();
Button b = new Button();
b.getStyleClass().add("run-button");
b.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent evt)
{
System.err.println("Got a button action");
}
});
v.getChildren().addAll(b);
Scene sx = new Scene(v, 200, 200);
sx.getStylesheets().add(Main.class.getResource("styleControl.css").toExternalForm());
stage.setScene(sx);
stage.setVisible(true);
where the styleControl.css file looks like this
.run-button
{
-fx-graphic: url("file:resources/cnr.png");
}
In this case clicking on the non-graphic part of the button causes the "Got a button action" message to appear, but clicking on the graphic part of the button does not (strangely two clicks on the graphic part does cause the message to appear).
It appears that the process of creating the graphic image when using the CSS style sheet is not the same as the default ImageView creation code. This makes CSS styling a lot less useful....