Change title bar and window icon

3

I want to change the icon of the title bar of the Java window, as shown below:

I'mgoingintheinitComponents()methodandleavingitasitisinthecodebelow,butthesetIconImage(iconeTitulo);lineisanerror.

publicFrmConfiguracoes(){initComponents();URLcaminhoImagem=this.getClass().getClassLoader().getResource("smile.png");
    Image    iconeTitulo = Toolkit.getDefaultToolkit().getImage(caminhoImagem);
    setIconImage(iconeTitulo);

    }

Does not recognize the command and does not change the icon. What can I do? And how to change the logo that is in the minimized icon, shape image below?

    
asked by anonymous 23.08.2016 / 04:41

1 answer

1

When taking the URL of the image, remove the getClassLoader() call as follows:

public FrmConfiguracoes() {

    initComponents();

    URL caminhoImagem = this.getClass().getResource("smile.png");
    Image    iconeTitulo = Toolkit.getDefaultToolkit().getImage(caminhoImagem);
    setIconImage(iconeTitulo);
}

Keep in mind that it will still work if the class and image are in the same project directory.

Another way is to make the call using the ImageIO :

public FrmConfiguracoes() {

    initComponents();

    try {

        Image iconeTitulo = ImageIO.read(getClass().getResource("smile.png"));

    } catch(IOException ex) {
      System.out.println("Erro ao importar icone: " ex.getMessage());
    }         
    setIconImage(iconeTitulo);
}

Both forms already change the icon of the window and also the icon that is displayed in the taskbar of windows, displaying the image that you pointed out in your project, see an example in the image below:

    
24.08.2016 / 19:50