Joseph Malachosky
Joseph Malachosky

Reputation: 85

JavaFX 8 Taskbar Icon Listener

DESCRIPTION: I created a javafx 8 application with the StageStyle.Undecorated window styling. I currently have a minimize, maximize and fullscreen button, as well as a key combination listener for maximizing the window.

QUESTION: Is there a way to attach a listener to the taskbar icon, so in turn I can minimize the application window by left-clicking on the icon?

Upvotes: 0

Views: 964

Answers (1)

Eudy Contreras
Eudy Contreras

Reputation: 396

enter image description hereAdd an OnMousePressed event handler to the node containing your icon, then add the method you wish to execute to the handler!

Example of a taskbar!!

public static HBox createButtons(String id, int amount, int width, int height) {
        Rectangle[] button = new Rectangle[3];
        DropShadow glow = new DropShadow();
        HBox buttonBox = new HBox(10);

        glow.setSpread(.6);
        glow.setRadius(10);

        buttonBox.getStylesheets().add(Styles.styledToolBarCss);
        buttonBox.setId(id);
        buttonBox.setAlignment(Pos.TOP_RIGHT);

        button[0] = new Rectangle();
        button[0].setFill(new ImagePattern(ImagesAndIcons.minimize));
        button[0].setWidth(width);
        button[0].setHeight(height);
        button[0].setOnMouseEntered(e ->{
            glow.setColor(Color.DODGERBLUE);
            button[0].setEffect(glow);
        });
        button[0].setOnMouseExited(e ->{
            button[0].setEffect(null);
        });
        button[0].setOnMouseReleased(e ->{
            TileMapEditor.minimize();
        });

        button[1] = new Rectangle();
        button[1].setFill(new ImagePattern(ImagesAndIcons.maximize));
        button[1].setWidth(width);
        button[1].setHeight(height);
        button[1].setOnMouseEntered(e ->{
            glow.setColor(Color.DODGERBLUE);
            button[1].setEffect(glow);
        });
        button[1].setOnMouseExited(e ->{
            button[1].setEffect(null);
        });
        button[1].setOnMouseReleased(e ->{
            TileMapEditor.maximize();
        });

        button[2] = new Rectangle();
        button[2].setFill(new ImagePattern(ImagesAndIcons.exit));
        button[2].setWidth(width*1.5);
        button[2].setHeight(height);
        button[2].setOnMouseEntered(e ->{
            glow.setColor(Color.RED);
            button[2].setEffect(glow);
        });
        button[2].setOnMouseExited(e ->{
            button[2].setEffect(null);
        });
        button[2].setOnMouseReleased(e ->{
            TileMapEditor.exit();
        });

        buttonBox.getChildren().setAll(button);

        return buttonBox;
    }

Upvotes: 2

Related Questions