0

I'm trying to add a border to my Image/ImageView, but I don't know how to do it.

When I press the button three randomly generated images are added to the GridPane. I've tried adding -fx-border-color and -fx-border-width properties through an external CSS file and then applying it but that way hasn't worked.

Any help is appreciated!

       btnLever.setOnAction((ActionEvent e) ->
       {
            // New array for loading in the images from 'generateImages' method
            String[] fruits = new String[3];
            fruits = generateImages();

            // GAME WILL RUN IF THE CREDITS IS GREATER THAN 0
            if(credits >0)
            {   
                // IMAGES GENERATED
                Image img1 = new Image(fruits[0]);
                Image img2 = new Image(fruits[1]);
                Image img3 = new Image(fruits[2]);

                // ADDS IMAGES TO THE GRID
                grid.add(new ImageView(img1), 0, 0);
                grid.add(new ImageView(img2), 1, 0);
                grid.add(new ImageView(img3), 2, 0);
josef
  • 83
  • 9
  • 1
    The reason CSS didn't work is because `ImageView` is not a `Region`. You could wrap the `ImageView` in a `Region` (one of the subclasses) and then use the `Region` to create a "border" (either using a real border or using padding and a background color). – Slaw Mar 15 '19 at 14:27
  • @Slaw can you give me an example of a region? Can I do this using a GridPane? – josef Mar 15 '19 at 14:28
  • 1
    You're currently adding the `ImageView`s to the `GridPane` directly. What I'm suggesting is you put the `ImageView` in, for instance, a `StackPane` first then add the `StackPane` to the `GridPane`. You can then style the `StackPane`. – Slaw Mar 15 '19 at 15:18
  • 1
    To avoid repetition I recommend using a method to create the `ImageView` + wrapper region. – fabian Mar 15 '19 at 15:28
  • [*JavaFX css border-radius issue*](https://stackoverflow.com/q/38004410/230513) may be helpful. – trashgod Mar 15 '19 at 17:47

0 Answers0