I am creating an windows desktop swt application.
I need to change the frame icon, for that I used
frame.setIconImage((new ImageIcon("C:\\Documents and Settings\\arjuns\\Desktop\\logo1 copy.png")).getImage());
The icon is displaying when I manually run the code from eclipse, but when I create an installer using Install4j the icon is not appearing.
Can anyone please help me.
URL url = ClassLoader.getSystemResource("ressources/logo.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
setIconImage(img);
This is similar to the previous answer, but I need to add a bit of information.
You can still use a direct path to your image (C:/User/logo.png) BUT imagine you give your program to someone else, he wont have the image in that specific path.
So I recemmend you insert it in your project like so:
(I usualy do a sperate package for any ressources).
so it will become ressources/logo.png and it will work for anybody opening your project.
import java.awt.*;
import javax.swing.*;
class set extends JFrame
{
set()
{
setSize(100,100);
setVisible(true);
setIconImage(new ImageIcon("navbit-home.png").getImage());
}
public static void main (String[] args) {
new set();
}
}
please set appropriate path.like C:/Documents and Settings/arjuns/Desktop/logo1copy.png
The image should be available in the JAR file you create. Then use getResource() to get the image from the jar file.
For example,
URL resource = this.getClass().getResource("resources/logo.png");
frame.setIconImage(new ImageIcon(resource).getImage());
Here the logo.png is located under 'resources' folder of the class file where this code is executed.
Related
I'm trying to create one simple GUI based testing tool in Java using Eclipse. I have been trying to add icon to my application. Images are present inside the project in a folder. Could you please suggest what mistake I'm doing.
I'm used below two ways, unfortunately both are not helping me. -
1.) frame.setIconImage(image).
2.) setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Filepath)));
Below is the setup of my project in Eclipse -
Below is code which i'm using -
public static void main(String[] args) {
JFrame frame = new JFrame("Testing Tool");
// setting close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// sets 500 width and 600 height
frame.setSize(500, 600);
try {
Image image = new ImageIcon("/Project_T/Images/biplane.jpg").getImage();
frame.setIconImage(image);
} catch(Exception e) {
System.out.println("Application icon not found");
}
// uses no layout managers
frame.setLayout(null);
// makes the frame visible
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Since i'm going to create an .exe file for this project using lunach4J, is this the way of keeping files (placing them in a folder of project since I would be using multiple images and files) that ensures the application running on any machine.
This is the code I used with a Swing application packaged in an executable JAR file. The JFrame has the icon image.
private static final String APP_ICON_PATH = "myapp/icondirectory/report.png";
ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource(APP_ICON_PATH));
frame.setIconImage(icon.getImage())
The "myapp" is the root of my package structure; e,g., the GUI class using the frame is in the package myapp.gui package. The image PNG files are within the application's executable JAR file (as you see in the code within a separate folder).
Also, look at the Oracle's Java Swing tutorials explain the usage of icons.
I fiddled a lot and finally found a way out , since my explanation was a bit long so I thought of writing an answer. Also please suggest that is this a good way since we would be exporting our project.
Java gives you the power to change the ICON for your application with the help
of setIconImage(image), but this method is not that direct, since we are
dealing with multiple files those should be present while executing the code so we
would have to ensure that their paths are correct and accessible in order to run smoothly.
Hence we save our files inside the src by creating another folder and from there
we can import the files easily. Follow these steps
Step - 0 - Place the files inside src folder of the project
Put the files inside the scr folder by creating another folder, here I created another folder by the name of images and placed my files there, hence the file path would be
"Images/biplane.png",
please ensure that there are no "/" placed before Images (our folder name)
Step - 1 - Place the file path inside a URL variable
Step - 2(Optional) - Print this URL variable to cross check that this is not null. If this is null check your path again and repeat the activity so that this value is not null.
Step - 3 - Now pass this URL to ImageIcon method.
Step - 4 - Call the setIconImage method with that image.
Code Used -
URL url = test.class.getClassLoader().getResource("Images/biplane.png");
System.out.println("Path for file is :- \"" + url + "\"");
Image image = new ImageIcon(url).getImage();
frame.setIconImage(image);
I am working on GUI java project which contains FileChooser(combined with JLabel it becomes ImageChooser) and JTextArea (inside of JScrollPane). Both of these components are inside of JPanel.
When ever I ran it inside of IntelliJ Idea (version 2017.2.4)everything works fine:
UI when executed from IDE
But if I build Artifacts and create .jar file, then image inside of JLabel is not initialized and the size(height) of JTextArea becomes minimal(though minimal value is set to 200):
IU when executed from .jar file
I suspect that ImageIcon cannot be initialized due to relative path I provide:
...
imagePath = "src/main/resources/" + item.getImageName();
//item.getImageName() returns a proper image name, tested with
//System.out.println() and there is a proper image in that folder.
ImageIcon img = new ImageIcon(imagePath);
img = ImageManager.resize(img);
...
//Resize function in ImageManager class
public static ImageIcon resize(ImageIcon imageIcon, int size){
return resize(imageIcon, size, size);
}
public static ImageIcon resize(ImageIcon icon){
return resize(icon, defaultSize);
}
However, I've tried options with relative path like main/resources/ and /main/resources/ , but none of them worked both in IDE and .jar executable.
Is it a problem with a path?
If yes, why does it affect JTextArea's size?
P.S.
JTextArea's size becomes normal if there is an image in JLabel.
You are right, the way you fetch resources is problematic in a jar.
The way you should access them:
ImageIcon img = new ImageIcon(this.getClass().getClassLoader().getResource(item.getImageName()));
This method supports relative paths. Just make sure your src/main/resources directory is properly marked as a 'Resource Root' in IntelliJ IDEA.
I made a custom Eclipse plugin that uses and displays multiple dialogs, and I want to know if I could set the top left icon image with the one I use in the plugin's icons folder. I want to get that icon and set it instead of the default one that Eclipse uses.
I'm overriding the configureShell() method to change the dialog title, and I also want to change the icon.
#Override
protected void configureShell(Shell parent){
super.configureShell(parent);
parent.setText("Choose variant...");
Image icon = new Image(parent.getDisplay(), "icons/best.gif"); - this method does not work as it cannot find the file
parent.setImage(icon);
}
I also tried using the getClass().getResource("best.gif") and having the image in the same package, still can't find the location I'm giving(FileNotFoundException), and also, the Image constructor does not accept URL objects.
#Override
protected void configureShell(Shell parent){
super.configureShell(parent);
parent.setText("Choose variant...");
Image icon = new Image(parent.getDisplay(), getClass().getResource("icons/best.gif"));
parent.setImage(icon);
}
Is there a way to use the icon that I already have in my eclipse plugin?
The main problem is getting the icon from the icons folder of the plugin and making it a Image object.
Thank you.
You can register the icon in your plugins activator class like this:
#Override
protected void initializeImageRegistry(final ImageRegistry reg) {
reg.put(IMAGE_PATH, imageDescriptorFromPlugin(PLUGIN_ID, IMAGE_PATH));
}
The image path is relative to your plugin, e.g. icons/icon.png.
You can access these images via the activator class as well:
final Image image = MyActivatorClass.getDefault().getImageRegistry().get(IMAGE_PATH);
myShell.setImage(image);
(Note that I used the image path as key in the image registry, you do not have to do it like this but it makes everything a little bit less complicated by just using the same static String.)
For an Eclipse plugin you use the FileLocator class to find resources in your plugin.
For an image use something like:
String path = "icons/best.gif";
URL url = FileLocator.find(bundle, new Path(path), null);
ImageDescriptor desc = ImageDescriptor.createFromURL(url);
Image image = desc.createImage();
Note: You must arrange for the image to be disposed when no longer needed.
If your activator extends AbstractUIPlugin you can also use the ImageRegistry available from that. The registry will deal with disposing.
Be sure that the icons directory is listed in the build.properties file. Missing this will causes issues when you export your plugin.
I have a method in which i use gwt PopupPanel to display an image while screen loads and server workloads.
final static PopupPanel popup = new PopupPanel(false, true);
The method i use is :
public static void sBanner() {
popup.setWidget(new Image("Images//Progress.gif"));
popup.setGlassEnabled(true);
popup.center();
}
What i dont manage to do it is to show the image when the method its called.
In local, runs fine; but when i deploy the code to Google App Engine, I get an icon like Google App Engine isnt finding the referenced file.
I have also tried to upload the image to Google Drive and use the sharing url as the url to use, but so far i havent been successful.
Anyone can enlight me?.
Thank you.
Use com.google.gwt.resources.client.ImageResource to provide images.
progress.gif should be in the same folder of Resources class (relative path can also be used-change #source value)
public interface Resources extends ClientBundle {
#Source("progress.gif")
ImageResource getPreloader();
}
Resources resources = GWT.create(Resources.class);
popup.setWidget(new Image(resources.getPreloader()));
I am using the NetBeans GUIBuilder to make a JPanel Form. I added a JLabel and used NetBeans' interface to give it an icon from an external image (.png). The path is verified and the image shows up on the GUIBuilder screen. It even shows up when I click the "Preview Design" button. It DOES NOT show up when I RUN the project. The rest of the GUI appears as it should. Do any of you know why this happening and/or how to fix it?
A lot of you have been asking for an SSCCE. Since the code is generated by the NetBeans Form Builder, I have instead included the steps I took to make the JLabel. The areas of focus are circled in red.
Drag and drop a JLabel into the Form Builder.
Open up the JLabel's properties menu. Enter the empty string ("") for the text field. Click the ellipsis next to icon.
Select External Image and click the ellipsis.
Select the image of choice. In my case it's a .png.
Notice that the image appears in the icon preview.
Close the icon menu and the properties menu, and notice that the image appears as the JLabel's icon on the Form Builder.
Thank you for accepting an unorthodox SSCCE and thank you in advance for your help.
I found out the hard way that relying on Netbeans GUI builder to do everything for you is a mistake.
Just create an icon fetching class like the one below, put the icons in it's package, and use "Custom code" instead of "Image chooser". Sure the icons will not be visible inside NB. But if they show up when the app is running, who cares about that.
package com.example.resource.icons;
import javax.swing.ImageIcon;
public class IconFetch {
private static IconFetch instance;
private IconFetch(){
}
public static IconFetch getInstance() {
if (instance == null)
instance = new IconFetch();
return instance;
}
public ImageIcon getIcon(String iconName) {
java.net.URL imgUrl = getClass().getResource(iconName);
if (imgUrl != null) {
return new ImageIcon(imgUrl);
} else {
throw new IllegalArgumentException("This icon file does not exist");
}
}
public static final String MINESWEEPER_ONE = "one.png";
}
Usage:
IconFetch.getInstance().getIcon(IconFetch.MINESWEEPER_ONE);
If the icon still doesn't show up after trying this, then something might be wrong with the way you layed out components in your form (the label is there but you can't see it).
Hope this helps even though it's a long shot.
I had the same problem, and predi's solution wasn't working either. Then I created a package instead of a folder, and added the images there, and it works now.
I do have a same problem also. But I found the solution.
I create the package in project and put the images inside there.
When I build the project, Netbeans will create 'target' folder and build .class files.
I found that the images that I copied to the package, did not transfer to the 'target' folder.
Interim solution.
4. I copy all image to target folder with the same structure. Then I can run the project directly from Netbeans.
5. Incase you clean the project. Do no.4 again.