# Set window icon in lwjgl 3.1

**URL:** <https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863>\
**Category:** support\
**Created:** [February 27, 2017, 4:44pm UTC](https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863 "2017-02-27T16:44:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArtemisHD](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/artemishd/32/92_2.png) [@ArtemisHD](https://discourse.glfw.org/u/ArtemisHD)\
**Post date:** [February 27, 2017, 4:44pm UTC](https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863/1 "2017-02-27T16:44:41Z")

</div>

I’ve been trying to set a png icon for my application (both taskbar and title bar) using the following example:

```
GLFWImage images[2];
images[0] = load_icon("my_icon.png");
images[1] = load_icon("my_icon_small.png");
glfwSetWindowIcon(window, 2, images);

```

The problem here is load\_icon does not exist. What does that method look like? How do I create a GLFWImage object from a .png resource? Googling won’t show any viable solutions and nobody seems to know how to do it. I would really appreciate some help. Thank you!

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [February 27, 2017, 5:08pm UTC](https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863/2 "2017-02-27T17:08:48Z")

</div>

The [icon format is a raw byte image](http://www.glfw.org/docs/latest/structGLFWimage.html) which you’ll need to load using an image loader library.

It looks like you can use the excellent [std\_image library via lwgl 3](https://javadoc.lwjgl.org/org/lwjgl/stb/STBImage.html) to load your image, and then create the GLFWImage from that.

---

<div class="post-metadata">

**Author:** ![ArtemisHD](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/artemishd/32/92_2.png) [@ArtemisHD](https://discourse.glfw.org/u/ArtemisHD)\
**Post date:** [February 27, 2017, 5:31pm UTC](https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863/3 "2017-02-27T17:31:39Z")

</div>

This is what I did, still need to get a GLFWImage object from that. Will post it as soon as it’s ready. This work is licensed under MIT.

```
public class image_parser {
    public static ByteBuffer load_image(String path) throws resource_error {
        ByteBuffer image;
        try (MemoryStack stack = MemoryStack.stackPush()) {
            IntBuffer comp = stack.mallocInt(1);
            IntBuffer w = stack.mallocInt(1);
            IntBuffer h = stack.mallocInt(1);

            image = stbi_load(path, w, h, comp, 4);
            if (image == null) {
                // throw new resource_error("Could not load image resources.");
            }
        }
        return image;
    }
}
```

---

<div class="post-metadata">

**Author:** ![ArtemisHD](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/artemishd/32/92_2.png) [@ArtemisHD](https://discourse.glfw.org/u/ArtemisHD)\
**Post date:** [February 27, 2017, 6:06pm UTC](https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863/4 "2017-02-27T18:06:11Z")

</div>

Its done and its working, thanks for helping me out, I am posting my full code for others to freely use. This work is licensed under MIT.

Image Parsing class:

```
public class image_parser {
    public ByteBuffer get_image() {
        return image;
    }

    public int get_width() {
        return width;
    }

    public int get_heigh() {
        return heigh;
    }

    private ByteBuffer image;
    private int width, heigh;

    image_parser(int width, int heigh, ByteBuffer image) {
        this.image = image;
        this.heigh = heigh;
        this.width = width;
    }
    public static image_parser load_image(String path) {
        ByteBuffer image;
        int width, heigh;
        try (MemoryStack stack = MemoryStack.stackPush()) {
            IntBuffer comp = stack.mallocInt(1);
            IntBuffer w = stack.mallocInt(1);
            IntBuffer h = stack.mallocInt(1);

            image = stbi_load(path, w, h, comp, 4);
            if (image == null) {
                // throw new resource_error("Could not load image resources.");
            }
            width = w.get();
            heigh = h.get();
        }
        return new image_parser(width, heigh, image);
    }
}

```

Main class:

`private final image_parser resource_01 = image_parser.load_image("icon_01.png");`

```
public void init() {
        if (!glfwInit()) System.out.println("Could not initialise opengl4.5.");

        glfwWindowHint(GLFW_RESIZABLE, 0);
        glfwWindowHint(GLFW_DECORATED, 0);

        int width = 1200;
        int height = 800;
        window = glfwCreateWindow(width, height, "My Window", NULL, NULL);
        if (window == NULL) System.out.println("Could not create our window.");

        glfwSetWindowPos(window, 100, 100);
        glfwShowWindow(window);
        glfwMakeContextCurrent(window);
        
        GLFWImage image = GLFWImage.malloc(); GLFWImage.Buffer imagebf = GLFWImage.malloc(1);
        image.set(resource_01.get_width(), resource_01.get_heigh(), resource_01.get_image());
        imagebf.put(0, image);
        glfwSetWindowIcon(window, imagebf);
    }
```

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [February 27, 2017, 6:58pm UTC](https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863/5 "2017-02-27T18:58:21Z")

</div>

Thanks for including your solution!

---

<div class="post-metadata">

**Author:** ![appstation](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/appstation/32/647_2.png) [@appstation](https://discourse.glfw.org/u/appstation)\
**Post date:** [July 17, 2022, 10:29pm UTC](https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863/6 "2022-07-17T22:29:48Z")

</div>

> [@ArtemisHD](#):
>
> `stbi_load(path, w, h, comp, 4);`

Hello !  
I studied your solution and get  
stbi\_load(path, w, h, comp, 4); … is undefined  
How do i have to handle that ?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 18, 2022, 7:50am UTC](https://discourse.glfw.org/t/set-window-icon-in-lwjgl-3-1/863/7 "2022-07-18T07:50:47Z")

</div>

Hi @appstation,

welcome to the GLFW forum.

To use the [STBImage class](https://javadoc.lwjgl.org/org/lwjgl/stb/STBImage.html) you need to import it in Java.

If you want to load all the functions the easiest way is:

```
import static org.lwjgl.stb.STBImage.*

```

The static keyword is used here so you don’t have to type the `STBImage.stbi_load()` but can just type `stbi_load()`.
