So, I am creating a GUI interface for the project via Netbeans (using it is a requirement). Everything is fine until I add an Icon for a label. The generated code is below:
jLabel25 = new javax.swing.JLabel();
jLabel25.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/mycompany/sostarnyba/images/map.png")));
jLabel25.setText("");
I add the icon exactly as the person in this question shows it: Netbeans ImageIcon not displaying
Except my app does not run at all.
When the icon is removed, everything runs, but when it is added it does not. The error is: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null.
What am I doing wrong?
The problem was solved by putting not a full path within the project but the absolute path in your computer starting from /Users
Related
Sorry newbie here. I get an error in Netbeans while trying to add an Icon to a J label. The error is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
After importing all of the images I used for my project. I still get this error.
This is the code in default in Netbeans:
diceButton = new javax.swing.JButton();
diceButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/dice.png"))); // NOI18N
(https://i.stack.imgur.com/VCANk.png)
I'm expecting to add it to a jlabel so that the image will also be at the jar file
You should also try this method to better understand label and button's icon (image) property.
Using absolute path, like my image in D drive in img folder,
ImageIcon icon = new ImageIcon("D:\\img\\img-name.jpg");
img_label.setIcon(icon);
Or you can also do this from label/image icon- property by specifying absolute path.
I'm new at Java and also swing. I just created a small app using JFrame and added some buttons and textFields, also I have a method which set the icon that I want for the taskbar and the one in the left corner.
When I run the program in Netbeans everything seems correctly, but when I build the project the icon it's not showing up. I tried a lot of things but none of them worked for me.
here's the method that I use for the program:
private void setIcon() {
ImageIcon imageIcon = new ImageIcon("src/main/java/icons/steam.png");
this.setIconImage(imageIcon.getImage());
}
And I call the method from the constructor.
Thank you.
EDIT 1:
Implementing what Andrew said, now I have this:
BufferedImage img = null;
try {
URL url = getClass().getResource("src/main/java/icons/steam.png");
img = ImageIO.read(url);
} catch (IOException e) {
}
this.setIconImage(img);
And that's on the constructor. But when I run it I get:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1400)
I tried different paths but I can't get it. And yes, I'm sure that "steam.png" is there.
I been looking for a solution to this and I finally get it (thanks to Andrew trough the comments)
First I edited my code as you can see in EDIT 1
After that, I got an IllegalArgumentException and the problem was that I didn't have a "resources" folder under /src
So I created my resources folder under src/main/resources and put my image inside
Then I got it using
URL url = getClass().getResource("/icons/steam.png");
img = ImageIO.read(url);
And that was the fix for my problem, now when I run the program images are now loaded.
Thank you so much!
The WindowBuilder is broken I can't add some of the components. When I try to do it:
Here is the error message
And when I try to add the component
Widow Builder was not able to show the GUI.
java.lang.NullPointerException
at org.eclipse.wb.internal.swing.java6.model.SwingVisualMapper.getComponentPreferredSize(SwingVisualMapper.java:85)
at org.netbeans.modules.form.layoutdesign.LayoutDesigner.endMoving(LayoutDesigner.java:596)
at org.eclipse.wb.internal.layout.group.model.GroupLayoutSupport.command_create(GroupLayoutSupport.java:277)
at org.eclipse.wb.internal.layout.group.gef.GroupLayoutEditPolicy2$4.executeEdit(GroupLayoutEditPolicy2.java:510)
at org.eclipse.wb.core.gef.command.EditCommand$1.run(EditCommand.java:52)
at org.eclipse.wb.internal.core.utils.execution.ExecutionUtils.run(ExecutionUtils.java:324)
at org.eclipse.wb.core.gef.command.EditCommand.execute(EditCommand.java:50)
at org.eclipse.wb.internal.gef.core.EditDomain.executeCommand(EditDomain.java:59)
at org.eclipse.wb.gef.core.tools.Tool.executeCommand(Tool.java:209)
I tried to add it manually and it worked.
The other problem is when I try to add any component, it creates it and after the creation Window Builder stuck. To fix it I have to return back to the source code wait 1-2 minutes. And I have to do this every time after I add new component.
This happens when I try to add a JLabel
I don't know if these bugs are because of Linux or it's just something wrong in my java/eclipse configuration. Are there any solutions?
I've been running into a problem lately where I try to se a JMenuItems icon which when I define and point to the icon the application it self don't start/show.
I started looking for errors, but there where none; started looking for write occurencies, which typically pretty much does appear when I add the icon and after that as pointed above the application doesn't start/show.
When the icon is set but commented:
Code
mntmMaximize = new JMenuItem();
mntmMaximize.setText("Maximize Window");
mntmMaximize.setActionCommand("maximize");
mntmMaximize.addActionListener(this);
mntmMaximize.setMnemonic(KeyEvent.VK_PLUS);
mntmMaximize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK));
// mntmMaximize.setIcon(new ImageIcon(Vision.class.getResource("xray/fullscreen16x.png")));
mnWindow.add(mntmMaximize);<br>
Picture:
Screen Shot Of Visible Application
After the icon is set and trying to execute application:
Code:
mntmMaximize = new JMenuItem();
mntmMaximize.setText("Maximize Window");
mntmMaximize.setActionCommand("maximize");
mntmMaximize.addActionListener(this);
mntmMaximize.setMnemonic(KeyEvent.VK_PLUS);
mntmMaximize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK));
mntmMaximize.setIcon(new ImageIcon(Vision.class.getResource("xray/fullscreen16x.png")));
mnWindow.add(mntmMaximize);<br>
Picture:
The window not created and application terminated
Note that when the window is not created in this picture the application is therefor terminated.
Please try to answer nice, and if you need the whole source file it is possible.
Edit:
Also if needed i can make a video where i show when i start the application when the icon is set but not commented.
getResource uses the relative path with respect to the package (folder), like in
Vision.class.getResource("fullscreen16x.png")
or absolute like in:
Vision.class.getResource("/xray/fullscreen16x.png")
I have 6 JButtons on my GUI all have images on it,
when I compile and run the code, all images on JButtons show up perfectly
but in runnable JAR file, images on JButtons are not showing up.. how do I fix this problem?
I used this method in my code to show icons on JButtons
ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
jb1 = new JButton(SettingsIc);
jb1.setFocusPainted( false );
//jb1.setBorderPainted(false);
jb1.setContentAreaFilled(false);
This is how my GUI looks when I compile my code in Eclipse
This is how my GUI looks after executing Runnable JAR file
This (as pointed out by a number of people)
ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
Suggests that you are trying to load the images from the bin/images off the file systems. This is a relative path from the execution point of your application.
ImageIcon won't complain if the file does not exist.
If possible, you are better off embedding the resources within your Jar file (it will make it easier to deploy) and use something like getClass().getResource("/bin/images/settings.png") to load the images.
If possible, you should try using ImageIO.read(URL) to load your images, it will throw an exception if the resource pointed to by the File/URL does not exist (or is invalid).
Just keep the jar and images in the same folder and
keep
ImageIcon icon = new ImageIcon("image.jpg");
in the code