JavaFX window icon doesn't change - java

I try to change icon in my javaFX application. It starts but icon doesn't change in both application top left corner and in task bar. I use this code to change the icon:
Image icon = new Image(new File("window_icon.png").toURI().toString());
stage.getIcons().add(icon);
But it seems to have no effect at all (no exceptions as well). BTW: I'm sure the file exisits. I also tried different images and formats(.png, .ico and .jpg)
(I use ItelliJ 2021.3.2 on windows 10 and create scene with FXMLLoader)
What can be the problem?
Update: tried 16x16, 32x32, 64x64, image's original 256x256
using Image("file:window_icon.png") doesn't change anything

The solution that worked for me is using ResourceStream. Putting the source image into /src/main/resources/{packagename} folder made this line of code work:
stage.getIcons().add(new Image(getClass().getResourceAsStream("window_icon.png")))
Thanks to #JoopEggen for the hint

Related

How to set a javafx scene to not have a icon?

Is there a way for a JavaFX stage to NOT display an icon, for it to have no icon?
For example, default windows dialog messages. They have no icon but still display on the taskbar and still have title.
Sorry if this is a beginner question that is obvious to some, but it seems the ethernet is too small for my basic questions.
Maybe you can use a different StageStyle than the standard StageStyle.DECORATED e. g. like this:
primaryStage.initStyle(StageStyle.UTILITY); // or StageStyle.UNDECORATED
As far as i know there is no support for removing the stage icon on a stage with the decorated style.

Resizing the Stage / Scene to fit the Window

I am new in using JavaFX, and I am facing trouble fitting/sizing my application window.The program can be seen in the image shared below.
When I initially run my program, the size is too small and when i maximize it, the whole anchorpane floats to the left of the window as shown in second image.What I actually want to implement is shown in the third window.
I want to start my app initially as it is shown in the 3rd image, and when the window is resized, the scene must also stretch fitting the stage.
I am very new to JavaFx, so please post help with some example so I can get my app working
Make sure you did Clean and Build and then run the file

JInternalFrame cropped Look and feel on windows

I'm currently working on a stand-alone java application running on Windows 7 with Aero.
I'm trying to use the system look and feel but I got some trouble with the look of the JInternalFrame.
http://img11.hostingpics.net/pics/300466buginternalframe.png
This is a screenshot of the internalFrame demo from Oracle
As you can see, I use the system look-and-feel, but the lower border is cropped and the title buttons are too close of the right side of the window. Those also seems to be a bit cropped.
I search for a fix to those issues but the only advice I found is to reduce the buttons width by changing the UIManager properties:
UIManager.put("InternalFrame.titleButtonWidth", 10);
This line do reduce the buttons but they still seems to be cropped. And it does not fix the lower border issue.
How could I possibly fix these 2 issues using the system look-and-feel?
The only way I found to bypass this bug is to change the internalFrame close icon for a custom icon in the UIManger which is a bit smaller than the original close icon:
ImageIcon closeIcon = new ImageIcon(ImageIO.read(new File(iconPath)))
UIManager.put("InternalFrame.closeIcon", closeIcon);

How do you add Background Image in JPanel (NetBeans GUI Builder)

I have JTabbedPane with five tabs and each have Jpanel i want add different images for each panel in NetBeans IDE
Right click on your project and add a new package, name it resources. This will need to be done so Netbeans imports your picture into that folder
Add a JLabel to the Panel
Hightlight the JLabel and go to the Properties pane on the right
In the property that says icon click the ... button. That will take you to a dialog
Choose External Image radio button
Click the ... next to the file text field
Pick your Image and click OK.
Click Import to Project
Click OK, You should see the image in your graphic layout
Note this will only give you an Image, but doesn't really act as a background, because the JLabel is it's own component. I'm not really sure how to achieve a backgroud with GUI Builder. I'm not too familiar with the technology. Though if you were to write your own code, there are numerous answers here on SO that I'm sure you'll find useful. The only tricky thing about GUI Builder is that they have auto-generated code that you really can't play around with, which circumvents what I know about creating a background image.
NOTE : this only works for JLabels as JPanels don't use Icon. An alternative would be to hand write your own JPanel code in the constructor and draw the image, overriding the paintComponent method.
Change the layout of your Jframe to null.
Create a jlabel and cover whole jframe with it.
Add your image to the icon property of the inserted jlabel.
Change the layout of jframe back to free layout.
You are done 👍👍
It worked for me
May be you'll find this link useful.
This tutorial shows you how to use the IDE's GUI Builder to generate the code to include images (and other resources) in your application. In addition, you will learn how to customize the way the IDE generates image handling code.
Handling Images in a Java GUI Application
Basically, following are the steps.
Drag a Label to the JFrame
Add a new package (for the image to be stored)
Select Label and go to the Properties category
Select the Icon property and click 'Import to Project...'
Select the image and then the newly created package
In Properties window of Label, select text property and delete it.

Can image of any size and dimension be used as an icon in JFrame?

I am trying to set the icon that appears in the top-left corner of a frame as a custom image with .gif extension, instead of the default java icon. I tried using :
setIconImage(Toolkit.getDefaultToolkit().getImage("/jlab/mainframe/images/jLab.gif"));
However, whenever I run the application, the default java icon still appears. Can image of any size be used as an icon? if yes then kindly resolve my problem. Thanks in advance.
Try this (it will look in the classpath):
setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/jlab/mainframe/images/jLab.gif")));

Categories