Creating Button with Image in eclipse not working - java

I am trying to create a button with an image. The API can be found here.
Here is my code:
Button settings = new Button(swpContainer, SWT.PUSH);
settings.setText("Settings");
settings.setImage(new Image(null, "/myProject/icons/settings.png"));
And here is the exception I am getting..
org.eclipse.swt.SWTException: i/o error (java.io.FileNotFoundException: \myProject\icons\settings.png (The system cannot find the path specified))
I am right clicking the image in eclipse and getting the path from the properties.
Any help will be greatly appreciated!

Try this:
Button settings = new Button(swpContainer, SWT.PUSH);
settings.setText("Settings");
Image image = new Image(swpContainer.getShell().getDisplay(),
getClass().getClassLoader().getResourceAsStream("icons/settings.png")); //$NON-NLS-1$
settings.setImage(image);

Presuming myProject is your project name, and icons is a simple folder in the project, then saying new Image(null, "icons/settings.png"); is enough.
I also recommend:
Reading this
Properly managing your resources

If you are writing it for RCP application,try using ImageDescriptor and create image.
Image IMG_EXAMPLE = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
"/icons/settings.png").createImage();

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)).

How should I use getResource()?

I use getResource() to load the image, but I get nullpointer exception. Also I tried to use css but I get nothing.
My code for label:
Image mine = new Image(getClass().getResourceAsStream("bomb3.png"));
ImageView im = new ImageView(mine);
Label label = new Label();
label.setGraphic(im);
And for button:
btn.setStyle("-fx-background-image: url('flag.png')");
This code is located in Painter class.
My file tree:
If you use getClass().getResourceAsStream("bomb3.png") java search in the packetof your class. So if your class is Game, your resource must be in the packet: com.name.minesweeperClasses
Image mine = new Image(getClass().getResourceAsStream("bomb3.png"));
here path of bomb3.png is incorrect
Please mention the relative path :-
./resources/boob3.png

Cannot create image from getResourceAsStream() in JavaFX projects

I cannot create an image in any of my JavaFX projects using the following kind of code:
final String url = "line.jpg";
Image image = new Image(Config.class.getResourceAsStream(url));
because there is always a null pointer exception pointing to the second line. Obviously, I have checked that the image file is in the correct directory. I have tried example programs, some directly copied from these boards, but these also fail for the same reason.
I suspect I lack a resource in Netbeans or JavaFX but I can't figure out what is missing.
The only workaround appears to be to include the image file in a css stylesheet and link it to the program by setting the gui components id like this:
Button homeButton = new Button();
homeButton.setId("homebutton");
In the stylesheet there is:
#homebutton {
-fx-background-image: url("images/homebtn.jpg");
-fx-pref-width: 30;
-fx-pref-height: 30;
}
Its not ideal being forced into this solution and Swing seems far better at dealing with image files. I assume a bug in JavaFX that always causes the following to fail:
Image image = new Image(Config.class.getResourceAsStream(url));

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