show image in Jlabel using Image icon - java

I'm using a Jlabel imageIcon to display my image
when I set the path of the image in a local machine it works perfectly but when I change the path to an image which is located in an other machine(NAS Server in my case) nothing displays and yet no problem is raised can any one help me.
here is a code snippet:
ImageIcon imic= new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(imgshow.getWidth(), imgshow.getHeight(), Image.SCALE_SMOOTH));
imgshow.setIcon(imic);
Any help I'll be thankful

You said "NAS Server in my case". Could we assume it's a normal "Samba" share ?

Related

How can I add picture as a JButton background?

I'm trying to add an image to JButton so there will be some kind of icon. And I'm not sure where is my mistake. When I run the following code, there is no changes in how button looks.
ImageIcon img = new ImageIcon("img.bmp");
JButton button = new JButton(img);
frame.add(button);
I've already tried to debug my code, although there was nothing useful.
Where is img.bmp stored relative to the
"working directory"?
ImageIcon (String) expects to find a
file on the disk, in this case, in the root of
the "working directory". If you don't know
the working directory, add
System. out.println(System.getPro
perty("user.dir")); to your code,
the image you're trying to load should be
stored there. The problem with this though,
is the working directory can change. You'd
be better of using embedded resources

Why does my ImageView work on Windows but not on Mac

Here is the simple code for javafx in intellij:
Image image = new Image(url);
ImageView imageView = new ImageView(image);
movieBox.getChildren().add(imageView);
//checking
System.out.println(imageView.getImage().getUrl());
Textfield tx = new Textfield();
tx.setText("hi");
movieBox.getChildren().add(tx);
I copied the project over to intellij on my mac and everything works accept the images won't show. Here are the things I tried:
Checking that the URL is valid by printing out the imageview image url. It prints out a valid URL of an image i can open in my browswer
Adding a random textfield to the movieBox (a flowpane object) to make sure it works. And it does.
There shouldn't be any error in the code because it works in my windows intellij. It seems like a problem with the environment, but I haven't even got a clue how should i approach this problem. Any sort of advice will be nice, thanks!
Try downloading the image and using the path to the image on your disk, if it works then the problem will be with the url, otherwise its with the ImageView. If the problem is with the URL, post the one you are trying to use so the community can test it, and try another URL.
P.S. If you aren't manipulating the image you can just pass the path/url directly top the ImageView constructor.
public ImageView(String url)
Allocates a new ImageView object with image loaded from the specified URL.
The new ImageView(url) has the same effect as new ImageView(new Image(url)).

Java: Set iconimage without using full path

I'm creating a game in java, and I have some problems setting my icon image:
ImageIcon img = new ImageIcon("/Users/me/location/mygame/res/logo.png");
f.setIconImage(img.getImage());
This works fine, but instead of /Users/me/location/mygame/res/logo.png, I want /logo.png, as I do with all images except this one.
Example of a place when it works perfectly:
player_sheet = loader.loadImage("/player_sheet.png");
This example is from my Texture class.
If I put /logo.png in the path, there will be no icon at all.

After buiding java app, some GUI components does not work properly, but during IDE tests everything is fine. Why?

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.

Display Bitmap resource in MainScreen banner for BlackBerry

Hey guys,
I'm having trouble displaying my icon in the banner of my MainScreen. The Icon is located in the res/ directory. Here's my MainScreen code:
HorizontalFieldManager mngr = new HorizontalFieldManager(USE_ALL_WIDTH);
LabelField label = new LabelField("AACD");
BitmapField pic=new BitmapField(Bitmap.getBitmapResource("res/aacdIconSmall.PNG"),Field.FOCUSABLE);
mngr.add(pic);
mngr.add(label);
this.setBanner(mngr);
BrowserField browserField = new BrowserField();
add(browserField);
The AACD Label shows up above my BrowserField but my pic bitmap never shows up. Any ideas?
Your code is OK.
However I do suspect you did not add the image to your JDE project, so the image is not included in the resulting .cod file during building the project.
No need to use res folder
try Bitmap.getBitmapResource("img/aacdIconSmall.png")
the folder should be like this
res
- img
- aacdIconSmall.png
(i dunno is accdIconSmall.PNG is case-sensitive or not, have not tested yet, just make sure you are using the same upper and lower case)

Categories

Resources