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);
}
}