How to set image of my own choice say baby.jpg as an icon on the title bar of the jFrame..
Please also help with codes if required ,, i am newbie in java and NetBeans..
How to set image of my own choice say baby.jpg as an icon on the title bar of the jFrame..
Please also help with codes if required ,, i am newbie in java and NetBeans..
Nice find, . To make this future-proof and IDE-agnostic, prefer setting a list of icons at multiple sizes so Windows/Linux task switchers and HiDPI displays pick the best one automatically. Put PNGs on the classpath (not file paths) and load them via the class loader so it still works from a built JAR.
// images: /images/app-16.png, app-32.png, app-48.png, app-64.png, app-128.png
List<Image> icons = new ArrayList<>();
for (int size : new int[]{16, 32, 48, 64, 128}) {
URL url = getClass().getResource("/images/app-" + size + ".png");
if (url != null) {
try { icons.add(javax.imageio.ImageIO.read(url)); } catch (IOException ignored) {}
}
}
frame.setIconImages(icons); // best-fit icon chosen by the OS A few gotchas that trip people up:
getResource(...) must point to a resource packaged inside your project (e.g., under src/...). If it returns null, the path is wrong or the file was not copied into the build. The leading slash means "from the classpath root".java.awt.Taskbar:if (java.awt.Taskbar.isTaskbarSupported() && !icons.isEmpty()) {
java.awt.Taskbar.getTaskbar().setIconImage(icons.get(0));
} JFrame) so splash windows and task switchers pick it up.is right to ask for the solution to help others; the above pattern works the same whether you use NetBeans GUI Builder or create frames by hand, and it scales cleanly when you later package your app.
Other people will have the same problem, and will find this article, so please share yur solution with the rest of the DaniWeb community.
I have got the solution.
If you are using NetBeans IDE then it can be done in two steps.
step 1 .
In declaration write
Image img =(new ImageIcon(getClass().getResource("/images/sirte_ico.JPG"))).getImage();
here img is the object created , will be used in step 2 below .
"/images/sirte_ico.JPG" here images is the folder name under "src" folder and sirte_ico.JPG is picture name i had chosen to set as icon for frame at title bar.
Now,
Step 2.
In navigator window , right click on the "JFrame" you want to set icon for and then choose properties.
in "Other properties section" you will find an option "iconImage" and a small button , click on the small button , you can an option "Set Form's iconImage property using : " choose "custom code" from the scroll down menu .
Now you will see "Form.setIconImage( [EMPTY BOX] );
Type img in the box as the object made in step 1 .
Press OK ,
close the properties window.
DONE !!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.