Set an image for a menu item [message #496023] |
Sat, 07 November 2009 09:53 |
antonio Messages: 22 Registered: July 2009 |
Junior Member |
|
|
Hi,
I'm using SWT 3.5.1 and GTK+ 2.18 on Ubuntu Karmic Koala.
I set an image (small icons) in menu items like this:
menuItem.setImage(new Image(shell.getDisplay(), getClass().getResourceAsStream(imagePath)));
or using absolute path:
menuItem.setImage(new Image(shell.getDisplay(), "path/somewhere/image.png"));
but they doesn't work.
Do I miss something?
Thanks,
Julio
[Updated on: Sat, 07 November 2009 09:53] Report message to a moderator
|
|
|
|
|
Re: Set an image for a menu item [message #496189 is a reply to message #496023] |
Mon, 09 November 2009 10:22 |
Daniel Krügler Messages: 853 Registered: July 2009 |
Senior Member |
|
|
antonio wrote:
> Hi,
>
> I'm using SWT 3.5.1 and GTK+ 2.18 on Ubuntu Karmic Koala.
>
> I set an image (small icons) in menu items like this:
>
> menuItem.setImage(new Image(shell.getDisplay(),
> getClass().getResourceAsStream(imagePath)));
>
> or using absolute path:
>
> menuItem.setImage(new Image(shell.getDisplay(),
> "path/somewhere/image,png"));
>
> but they doesn't work.
>
> Do I miss something?
Never write it either way: Images (like Fonts and other
non-memory system resources) need to be explicitly dispose'd
by the user, if created on their own or if they use a
library function that says so.
If you don't want to depend on jface, you probably need to write
your own org.eclipse.jface.resource.ImageRegistry to perform
proper resource managment.
There may be several reasons for failing to set the image
explicitly ("doesn't work" is a very imprecise description
of what actually fails):
a) Your classes are part of a Eclipse plugin and
getResourceAsStream won't here, better use the functionality of
FileLocator to get the proper location of the image.
b) Your file path is invalid or not accessible for other reasons.
Check this by testing it with different file access functions
and try to isolate, which step fails.
Greetings from Bremen,
Daniel Krügler
|
|
|
|
|
Re: Set an image for a menu item [message #496510 is a reply to message #496023] |
Tue, 10 November 2009 11:48 |
antonio Messages: 22 Registered: July 2009 |
Junior Member |
|
|
Hi Daniel,
that's my snippet:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.Image;
public class DemoMenu {
public static void main (String [] args) {
final Display display = new Display ();
final Shell shell = new Shell (display);
Menu bar = new Menu (shell, SWT.BAR);
shell.setMenuBar (bar);
MenuItem fileItem = new MenuItem (bar, SWT.CASCADE);
fileItem.setText ("&File");
Menu submenu = new Menu (shell, SWT.DROP_DOWN);
fileItem.setMenu (submenu);
MenuItem item = new MenuItem (submenu, SWT.PUSH);
item.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event e) {
shell.dispose();
}
});
item.setText ("E&xit\tCtrl+Q");
item.setAccelerator (SWT.MOD1 + 'Q');
Image img = new Image(display, "gtk-close.png");
System.out.println("X: " + img.getImageData().width + " Y: " + img.getImageData().height);
item.setImage(img);
img.dispose();
shell.setSize (200, 200);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
I always use 16x16 image for icon menu. For the snippet above I have used:
http://docs.wxwidgets.org/trunk/gtk-close.png
Anyway snippet above doesn't display icon menu for "Exit" option.
(SWT 3.5.1 + GTK+ 2.18 on Ubuntu 9.10)
Thanks,
Julio
|
|
|
|
|
Re: Set an image for a menu item [message #496562 is a reply to message #496510] |
Tue, 10 November 2009 14:38 |
Grant Gayed Messages: 2150 Registered: July 2009 |
Senior Member |
|
|
Move img.dispose() down to just before display.dispose() ?
Grant
"antonio" <antongiulio05@gmail.com> wrote in message
news:hdbjtf$dgu$1@build.eclipse.org...
> Hi Daniel,
>
> that's my snippet:
>
>
> import org.eclipse.swt.*;
> import org.eclipse.swt.widgets.*;
> import org.eclipse.swt.graphics.Image;
>
> public class DemoMenu {
>
> public static void main (String [] args) {
> final Display display = new Display ();
> final Shell shell = new Shell (display);
> Menu bar = new Menu (shell, SWT.BAR);
> shell.setMenuBar (bar);
> MenuItem fileItem = new MenuItem (bar, SWT.CASCADE);
> fileItem.setText ("&File");
> Menu submenu = new Menu (shell, SWT.DROP_DOWN);
> fileItem.setMenu (submenu);
> MenuItem item = new MenuItem (submenu, SWT.PUSH);
> item.addListener (SWT.Selection, new Listener () {
> public void handleEvent (Event e) {
> shell.dispose();
> }
> });
> item.setText ("E&xit\tCtrl+Q");
> item.setAccelerator (SWT.MOD1 + 'Q');
> Image img = new Image(display, "gtk-close.png");
> System.out.println("X: " + img.getImageData().width + " Y: " +
img.getImageData().height);
> item.setImage(img);
> img.dispose();
> shell.setSize (200, 200);
>
> shell.open ();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
>
> }
>
>
> I always use 16x16 image for icon menu. For the snippet above I have used:
>
> http://docs.wxwidgets.org/trunk/gtk-close.png
>
> Anyway snippet above doesn't display icon menu for "Exit" option.
> (SWT 3.5.1 + GTK+ 2.18 on Ubuntu 9.10)
> --
> Thanks,
> Julio
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04263 seconds