Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8112204

Button graphic created differently when graphic made programmatically than when made via CSS styling

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • fx2.0
    • fx2.0
    • javafx
    • Mac OS X 10.5.8, B21 JavaFX EA

      (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....

            kwwong Kinsley Wong (Inactive)
            gmatthewsjfx Graham Matthews (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: