I have made sure that image path is correct and syntaxes are correct for adding a label to a frame.
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JLabel label = new JLabel();
label.setText("Hello");
ImageIcon nintendoImage = new ImageIcon("Images/nintendo.jpg");
label.setIcon(nintendoImage);
frame.add(label);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I have also tried to use Absolute path for the image(copying from Finder(Mac)), even tried putting the img in same package. And, yes I have all the necessary imports), but it didn't work
This image is of my project directory. I am running it from Vs Code, tried also from IntelliJ but it didn't help
I have made sure that image path is correct and syntaxes are correct for adding a label to a frame.
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JLabel label = new JLabel();
label.setText("Hello");
ImageIcon nintendoImage = new ImageIcon("Images/nintendo.jpg");
label.setIcon(nintendoImage);
frame.add(label);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I have also tried to use Absolute path for the image(copying from Finder(Mac)), even tried putting the img in same package. And, yes I have all the necessary imports), but it didn't work
This image is of my project directory. I am running it from Vs Code, tried also from IntelliJ but it didn't help
Share Improve this question edited Jan 20 at 4:12 Corvus-OS asked Jan 19 at 1:24 Corvus-OSCorvus-OS 11 bronze badge 10 | Show 5 more comments2 Answers
Reset to default 0Your code works just fine, if the image path you inputted is indeed correct.
On a bare project (without a dependency manager like maven/gradle etc) your file structure should be {ProjectFolder}/Images/nintendo.jpg
.
Maybe you are confused about what the Jframe.setIconImage()
does and you are expecting something different. It actually changes the top left icon/image of the app (JFrame).
The background JFrame (window) has the default Java Swing icon, while the foreground (highlighted) has a random Nintendo image applied by using the exact same code with the path described as above:
You'll never want to have a relative path in the file system for what is essentially a resource, which will be packaged and deployed with your app. So you should load it as a resource:
import java.awt.EventQueue;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class F extends JFrame {
private void setGui() throws IOException {
setLocation(0, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(ImageIO.read(F.class.getResource("/gvim.png")));
}
public static void main(String[] args) throws Exception {
// Make sure we have a LAF that HAS an icon
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
// Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
EventQueue.invokeAndWait(() -> {
try {
F f = new F();
f.setGui();
f.setSize(200, 200);
f.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
ImageIO
to read images.ImageIO
throws an exception if the image path is incorrect. Search "java resources folder" for the best way to package images in your project. – Gilbert Le Blanc Commented Jan 19 at 2:24new ImageIcon(String)
will create a pseudo image/icon even if the image was not found/loaded - you must check its width or height to confirm it was successfully loaded (it will have a no size if not loaded) - a good reason to useImageIO
(a litle less older thanImageIcon
) -- Tutorial How to Use Icons: "If the data location is invalid (but non-null), an ImageIcon is still successfully created; it just has no size and, therefore, paints nothing." – user85421 Commented Jan 19 at 8:58