Get applet to load pictures when user clicks on Level one [duplicate] - java

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.

Related

Adding ImageIcon to JTabbedPane

I am attempting to add a picture to the first tab of a JTabbedPane, here is my code:
JTabbedPane application = new JTabbedApplication();
JPanel welcomePanel = new JPanel();
JLabel imageLabel = new JLabel(new ImageIcon("track.jpg"));
welcomePanel.add(imageLabel);
application.addTab("WELCOME", welcomePanel);
application.setMnemonicAt(0, KeyEvent.VK_1);
The image file is located in the same location as the class this code is in. For some reason, however, my image is not appearing. I have used the same JLabel and used text instead of an image and it appears. Can someone give me some insight into this problem?
Java doesn't know that the image is located directly beside the running class file. You have to hand in the absolute path. Which means Path + filename.
This little piece of code will help you to tell Java that the image is in the current working directory.
String file = new File("track.jpg").getAbsolutePath();
Added to your code snipped it looks like this:
JTabbedPane application = new JTabbedPane();
JPanel welcomePanel = new JPanel();
String file = new File("track.jpg").getAbsolutePath();
JLabel imageLabel = new JLabel(new ImageIcon(file));
welcomePanel.add(imageLabel);
application.addTab("WELCOME", welcomePanel);
The image file is located in the same location as the class this code is in
Instead of JLabel imageLabel = new JLabel(new ImageIcon("track.jpg")); which is looking for a file in the current working directory, you should be using Class#getResource which will return a URL to the embedded resource.
It's important to note, that resources which are store within the application context (ie embedded inside the Jar or within the source packages) aren't accessible as "files" anymore.
Instead, you could try using something like...
ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResource("track.jpg"));
to load the image. Unlike ImageIcon, ImageIO.read will throw an IOException if the image can't be read, which is always more useful than the silence that you get from ImageIcon
If this fails to work, more context on the structure of your app will be needed

Import image GUI not appearing

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

how to display image using its path

i have a the path of an image in a string i want to display it on a jlabel using the path. please help me.
if(!ar.get(16).equals("empty")){
String photo=(String)ar.get(16);
System.out.println(photo);
// if(!photo.equals(""))
// pic.setText(photo);
Image image=Toolkit.getDefaultToolkit().getImage(photo);;
ImageIcon img=new ImageIcon(image.getScaledInstance(view.jLabel5.getWidth(), view.jLabel5.getHeight(), 0));
//JpegReader jrdr=new JpegReader();
//view.jLabel5.setSize(img, image.getWidth());
view.jLabel5.setPreferredSize(new Dimension(100, 100));
view.jLabel5.setIcon(img);
}
If the image is an embedded resources (ie lives within the application context/is bundled with the application Jar), then you need to use getResource to gain access to it..
Toolkit.getDefaultToolkit().getImage expects that the String passed to it is a file on the file system.
If the image is embedded, then you will need to use something more like...
Toolkit.getDefaultToolkit().getImage(getClass().getResource(photo))
To load it.
If the image is being loaded from the file system, you could use
File file = new File(photo);
if (file.exists()) {
// Attempt to load the image
} else {
// Show error message.
}
Because of the way Toolkit#getImage works, it will not provide any details if the image fails to load for some reason.
Instead, you should be using ImageIO, which will throw an IOException if it was unable to load the image for some reason...
BufferedImage img = ImageIO.read(getClass().getResource(photo));
or
BufferedImage img = ImageIO.read(new File(photo));
Depending on where the image is located.
Take a look at Reading/Loading an Image.
You should also avoid calling setPreferredSize explicitly and simply allow JLabel to make it's own choices...
Put the image file in the project. Under a seperate folder.
ImageIcon image = new ImageIcon(this.getClass().getResource("/images/abc.jpg"));
JLabel imageLabel = new JLabel(image, JLabel.CENTER);
If you want to load the image for any other location on your computer then,
ImageIcon image = new ImageIcon("C:/images/image.png");
JLabel imagelabel = new JLabel(image);
Make sure your paths are correct. If you photo string path is "photo.jpeg", your path should look something like this
ProjectRoot
photo.jpeg
src
If you want to put the photo.jpeg in a directory images, you should use "images/photo.jpeg"
ProjectRoot
images
photo.jpeg
src
This is considering you're using an IDE like Netbeans or Ecplise.

how to set page icon to Jfilechooser dialog

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.

Adding image in JApplet

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.

Categories

Resources