i am going to create an applet jar which has a JFileChooser open dialog. and i want to change its icon.
frame = parentFrame;
ImageIcon icon = new ImageIcon("com/biztree/docmntui/client/applet/favicon.gif");
frame.setIconImage(icon.getImage());
and then
int returnVal = fileChooser.showOpenDialog(frame);
it works fine when i run it as applet.
but when i trying to run it in GWT web page its shows java default icon.
new ImageIcon("com/biztree/docmntui/client/applet/favicon.gif");
The String based constructor for an ImageIcon interprets the string as a File path. I doubt that would work with GWT. It is probably expecting to deal with the resource by URL. To get an URL, do something like:
URL favIconUrl =
this.getResource("com/biztree/docmntui/client/applet/favicon.gif");
Use the URL instead of the String in the ImageIcon constructor.
Related
I am designing a Java program to set icon to JLabel when clicking the corresponding button. I want to know ways to check whether the img exists before calling:
ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource(path));
I'm not allowed to use try catch!
If the image does not exist in the supplied path, the URL returned from getResource(..) will be null.
I am trying to upload an image on a GUI. Here is what my code reads right now:
ImageIcon icon = new ImageIcon(nameSearched.getImage());
myLabelK = new JLabel();
myLabelK.setBounds(500,100,200,200);
myLabelK.setIcon(icon);
myPanel.setLayout(null);
myPanel.add(myLabelK);
Validate();
add(myPanel);
setVisible(true);
When I run my program this image is not popping up on my GUI.
The nameSearched.getImage() is calling a method in a different class which returns an image entered into the system. For example, Peter.jpg.
Please help me figure out how to get my image on the screen.
If you are just using Peter.jpg as you relative path name,
Using an IDE, you image should be ditrectly below the ProjectRoot directory
ProjectRootDir
Peter.jpg
src
If you wanted to have your image in an images folder:
use "images\Peter.jpg"
ProjectRootDir
images
Peter.jpg
src
Maybe you want to retrieve the image like this
ImageIcon icon = new ImageIcon("Peter.jpg");
Another possibility, If you do this:
ImageIcon icon = new ImageIcon(nameSearched.getImage());
then nameSearched.getImage() must return either a String fie path of the image
or an Image object.
Maybe you just want nameSearched.getImage() to return a String from an array of String paths. Not sure what nameSearched.getImage() actually does so I can't go into further explanation or correction
ImageIcon icon= new ImageIcon("a.gif");
JLabel jLabel1=new JLabel(icon);
jLabel1.setVisible(true);
card1.add(jLabel1);
I am a newbie to Java and I am facing a problem to add image in a panel in applet. My image is in the same folder. My applet is visible without any problem but only image is not displayed.
public void init()
URL imageURL = new URL(getDocumentBase(), "a.gif");
Image image = getImage(imageURL);
ImageIcon icon = new ImageIcon(image);
// ...
The ImageIcon constructor that accepts a String presumes the string represents the path & file name of a File.
Only trusted applets can access a File, and then only on the client file-system (not the server). If this is an application resource, it should be on the server, and can be accessed by URL.
Note that the ImageIcon constructor will also accept an URL, rather than the Image used above. I just wanted to highlight that applets have an inbuilt method to obtain images.
I am designing a GUI. I have created a Menubar and added Menu items to Menu and Have setMenubar.
My problem is i am not able to add an icon (icon is not appearing in eclipse) to the Menuitem.
This is how I did:
I have my .png file in D:/something/src/resources/new.png
JMenuBar menuBar = new JMenuBar();
JMenu File = new JMenu("File");
menuBar.add(File);
java.net.URL imageURL = this.getClass().getResource("/resources/new.png");
System.out.println(imageURL); //imageURL is printing correctly in console
ImageIcon im = new ImageIcon(imageURL);
JMenuItem newItem = new JMenuItem("New", im);
File.add(newItem);
setJMenuBar(menuBar);
I am facing the similar problem adding icon to a button in Toolbar too. Guess, it is same cause. Could anyone please tell me what I am doing wrong.
Note: I have tried with .jpg, .jpeg and .ico files too. But nothing is appearing in eclipse!. I am using Windows & MS Access database.
A file in resources would typically end up at the root of a Jar. Try:
java.net.URL imageURL = this.getClass().getResource("/new.png");
If that fails for you, expand the Jar and check the image is located where you think it is.
Hopefully the resources folder, is the Source Folder that you had created by right clicking your Project. So you can use this also to retrieve images.
URL url = ClassLoader.getSystemResource(path);
where path = "new.png"
Or as quoted by you :
"This is how I did: I have my .png file in D:/something/src/resources/new.png"
Try to change the location of the resources folder inside classes/bin folder instead of src. So it must look like this D:/something/classes/resources/new.png
Now if you will use
URL url = ClassLoader.getSystemResource(path);
then path = "resources/new.png";
Then i guess your way of doing will run too.
Hopefully this might help you in some way.
Regards.
ImageIcon icon= new ImageIcon("a.gif");
JLabel jLabel1=new JLabel(icon);
jLabel1.setVisible(true);
card1.add(jLabel1);
I am a newbie to Java and I am facing a problem to add image in a panel in applet. My image is in the same folder. My applet is visible without any problem but only image is not displayed.
public void init()
URL imageURL = new URL(getDocumentBase(), "a.gif");
Image image = getImage(imageURL);
ImageIcon icon = new ImageIcon(image);
// ...
The ImageIcon constructor that accepts a String presumes the string represents the path & file name of a File.
Only trusted applets can access a File, and then only on the client file-system (not the server). If this is an application resource, it should be on the server, and can be accessed by URL.
Note that the ImageIcon constructor will also accept an URL, rather than the Image used above. I just wanted to highlight that applets have an inbuilt method to obtain images.