Squittron
Squittron

Reputation: 23

Set border around imageview with no background in javafx

I would like to have a button or a clickable ImageView in my program. When clicked i would like to have a border to appear in a shape of the imageview. This image had no background, but i can't find a way to specify the border's shape. For example:

enter image description here

This image has no background, and the border has to be only around the image so no rectangles or circles. Is this possible?

Upvotes: 2

Views: 5186

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49185

You may prefer to use DropShadow effect to show border:

@Override
public void start( final Stage primaryStage )
{
    DropShadow ds = new DropShadow( 20, Color.AQUA );
    ImageView imageView = new ImageView( "http://vignette3.wikia.nocookie.net/forgeofempires/images/b/b8/Castel_del_Monte.png" );

    imageView.setOnMouseClicked( ( MouseEvent event ) ->
    {
        imageView.requestFocus();
    } );

    imageView.focusedProperty().addListener(( ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue ) ->
    {
        if ( newValue )
        {
            imageView.setEffect( ds );
        }
        else
        {
            imageView.setEffect( null );
        }
    });

    final Scene scene = new Scene( 
            new VBox( imageView, 
            new Button( "When you focus on me, the imageview looses its shadow effect" ) ), 
            500, 200 );
    primaryStage.setScene( scene );
    primaryStage.show();
}

When the imageview is clicked we request a focus on it, which triggers the focusProperty change listener and sets the effect, and when the imageview looses its focus (either by hitting TAB or clicking the below button) the effect cleared.

Upvotes: 6

Related Questions