3

I want to add an application icon to my program. I've tried to do that as following, but it haven't worked:

primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("lock.png")));
    primaryStage.getIcons().add(new Image(Controller.class.getResourceAsStream("lock.png")));
    final Parent root = FXMLLoader.load(getClass().getResource("passwordGenerator.fxml"));

or

primaryStage.getIcons().add(new Image("lock.png"));

Also I tried to do that without a FXML file and it worked.

How can I add an application Icon with a FXML file?

4
  • Possible duplicate of JavaFX Application Icon Commented Sep 1, 2017 at 21:26
  • Your image and your Main class are in the same package? Commented Sep 1, 2017 at 21:29
  • @MenaiAlaEddine This answer haven't worked for me Commented Sep 2, 2017 at 9:53
  • @MenaiAlaEddine My image and Main class are in the same package Commented Sep 2, 2017 at 9:54

1 Answer 1

0

here the solution it works with me : Main class :

public class JavaFXIcons extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);
    stage.getIcons().add(new Image(JavaFXIcons.class.getResourceAsStream("stackoverflow.jpg"))) ;
    stage.setScene(scene);
    stage.show();
}

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

FXML file:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxicons.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.