0

I just tried to put "user.png" file in Jlabel, and add it to JFrame. ("user.png" file exists in the same directory with the Java file)

Also, I think frame setIconImage also didn't work.

What should I do to fix it?

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;

class TestLabel {
  public static void main(String[] args){
    ImageIcon image = new ImageIcon("user.png");

    JLabel label = new JLabel();
    label.setText("This is a point icon");
    label.setIcon(image);
    label.setHorizontalTextPosition(JLabel.CENTER);
    label.setVerticalTextPosition(JLabel.TOP);
    
    JFrame frame = new JFrame("title");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.getContentPane().setBackground(new Color(0x00B890));
    frame.setVisible(true);
    frame.add(label);
  }
}

[edit version]

package basic;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Taskbar;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

class TestLabel {
  public static void main(String[] args){
    ImageIcon image = new ImageIcon(new BufferedImage(400,100,BufferedImage.TYPE_INT_RGB));

    JLabel label = new JLabel();
    label.setText("This is a point icon");
    label.setIcon(image);
    label.setHorizontalTextPosition(JLabel.CENTER);
    label.setVerticalTextPosition(JLabel.TOP);
    
    JFrame frame = new JFrame("title");
    Taskbar taskbar=Taskbar.getTaskbar();
    BufferedImage favicon = null;
    try{
      favicon = ImageIO.read(TestLabel.class.getResource("user.png"));
    }
    catch (IOException e){
      System.out.println("IOException occured");
    }
    
    taskbar.setIconImage(favicon);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.getContentPane().setBackground(new Color(0x00B890));
    frame.add(label);
    frame.pack();
    frame.setVisible(true);
  }
}

This is the execution result:

HAPPY
  • 1
  • 1
  • Try this experiment. Add `import java.awt.image.BufferedImage;` among the imports, then replace `new ImageIcon("user.png");` with `new ImageIcon(new BufferedImage(400,100,BufferedImage.TYPE_INT_RGB);`. Report back, do you see a black (400x100) icon in the frame? **BTW:** `frame.setVisible(true); frame.add(label);` should instead be `frame.add(label); frame.pack(); frame.setVisible(true);`. – Andrew Thompson Feb 08 '22 at 06:09
  • 1
    *"file exists in same directory with java file"* the the image is now an embedded resource, use `label.setIcon(new ImageIcon(ImageIO.read(TestLabel.class.getResource("user.png"))));` instead (this will now throw an exception which you will need to handle) – MadProgrammer Feb 08 '22 at 06:20
  • Yes, I can see a black icon in the frame! – HAPPY Feb 08 '22 at 07:08
  • @AndrewThompson then if I want to put some image there, what should I do? – HAPPY Feb 08 '22 at 07:13
  • 1
    *"what should I do?"* 1) Fix the other things I discussed after **BTW:** in the first comment. 2) [Edit] the question to show the new code. 3) Implement the advice as detailed by @MadProgrammer. It was obvious that the image path was the problem all along, I just hoped that by putting a standard image in the GUI, you would become aware of that. – Andrew Thompson Feb 08 '22 at 09:59
  • user.png should be in your current working directory. You can also try it out by giving absolute path for user.png in your program. – Hitesh A. Bosamiya Feb 08 '22 at 12:02
  • 1
    @HiteshA.Bosamiya Your advice will **not work** when the app. is deployed, whereas loading the image as an application resource using `getResource` will work both during development ***and*** at the time of deployment. – Andrew Thompson Feb 08 '22 at 12:05
  • Oh, thank you MadProgrammer, I can also solve the problem of changing the dock icon now. – HAPPY Feb 08 '22 at 14:00

0 Answers0