0

I am having trouble changing the application icon with JavaFX (code is below with my attempts commented out). I tried implementing several solutions from previous stack overflow answers but I'm not sure if those methods are now deprecated. I am using NetBeans 8.2 (and the icon is in a folder called images under the source package).

1st Attempt: Illegal start of expression. identifier expected: JavaFX Application Icon

2nd Attempt: No suitable method found for add(java.awt.Image): Changing the icon of my java application

3rd Attempt: Cannot find symbol. Cannot instantiate the type Image java?

5th Attempt: Image is abstract it cannot be instantiated. http://docs.oracle.com/javafx/2/deployment/self-contained-packaging.htm

package javafxapplication1;

import java.awt.Image;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javax.imageio.ImageIO;


public class JavaFXApplication1 extends Application {
    private double xOffset = 0;
    private double yOffset = 0;


    @Override
    public void start(Stage stage) throws Exception {

        //stage.getIcons().add(Image(<JavaFXApplication1>.class.getResourceAsStream( "/images/fiji.png" ));

        Image i = ImageIO.read(getClass().getResource("/images/fiji.png"));
        //setIconImage(i);
        //stage.getIcons().add(i);

        //stage.getIcons().add(Image("/images/fiji.png"));

        // stage.getIcons().add(ImageIO.read(getClass().getResource("/images/fiji.png")));

        //stage.getIcons().add(new Image(this.getClass().getResourceAsStream("/images/fiji.png")));

        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        //stage.initStyle(StageStyle.UNDECORATED);
        // makes it moveble
        // LOOK INTO!!!!!!!!!!!
         root.setOnMousePressed(new EventHandler<MouseEvent>() {
             @Override
             public void handle(MouseEvent event) {
                 xOffset = event.getSceneX();
                 yOffset = event.getSceneY();
             }
         });
         root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
            stage.setX(event.getScreenX() - xOffset);
            stage.setY(event.getScreenY() - yOffset);
            }
         });        
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
Piyin
  • 1,823
  • 1
  • 16
  • 23
Alhc
  • 1
  • 1

2 Answers2

2

You need to load an Image and add it to the stage's icons.

import javafx.scene.image.Image;
Image icon = new Image(Controller.class.getResource("/game.png").toExternalForm(), false);  
primaryStage.getIcons().add(icon);

However, on Ubuntu these icons do not get displayed. This JavaFX defect hasn't been solved for a long time.

It seems that your first attempt is missing the new keyword for the Image instantiation, and make sure it is a javafx.scene.image.Image, not a java.awt.Image image, which has a different constructor. Try this:

stage.getIcons().add(new Image(JavaFXApplication1.class.getResource( "/images/fiji.png" ).toExternalForm());
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
0

First, load an image and then add to the stage object. Please make sure to give path starting from the inside of the resource folder, not from the resource folder, or else use the whole project path.

Image favicon = new Image('URL_OF_THE_IMAGE');
stage.getIcons.add(favicon);