Change title bar and window icon

Asked

Viewed 1,116 times

4

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

inserir a descrição da imagem aqui

I’m going the method initComponents() and leaving as it is in the code below, but the line setIconImage(iconeTitulo); is left as a mistake.

public FrmConfiguracoes() {
        initComponents();

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

    }

It 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?

inserir a descrição da imagem aqui

1 answer

2


When retrieving the image URL, remove the call getClassLoader() 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 only work if the class and image are in the same project directory(package).


Another way is to call using the class 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 window icon and also the icon that is displayed in the windows taskbar, displaying the image you pointed out in your project, see an example in the image below:

inserir a descrição da imagem aqui

  • Thanks for the help. You are giving error. I used the code below: //changing logo URL pathImage = this.getClass(). getResource("/logo.bmp"); Image iconeTitulo = Toolkit.getDefaultToolkit(). getImage(pathImage); setIconImage(iconeTitulo); When running the program the error is given: Uncaught error fetching image: java.lang.Nullrepointexception

  • If I am not mistaken it has to be png or jpg the format, and this bar ai is unnecessary if the image is in the same package as the class.

  • @Rodrigostortideoliveira the above code solved the problem?

  • Thanks for the help. I tested with JPG without the bar but it didn’t work

  • @Rodrigostortideoliveira then the problem is with the path. If you’re using netbeans, drag the photo to the same package as your screen class, and just pass the name.extensao.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.