Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Set an image for a menu item
Set an image for a menu item [message #496023] Sat, 07 November 2009 09:53 Go to next message
antonio is currently offline antonioFriend
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 #496042 is a reply to message #496023] Sat, 07 November 2009 13:52 Go to previous messageGo to next message
govind Ra is currently offline govind RaFriend
Messages: 95
Registered: July 2009
Member
Hi ,,

Add IAction to menu manager //
menumager.add(new CustomAction());

and override getImageDescriptor method withe ur image.

Regards
Govind R
Re: Set an image for a menu item [message #496051 is a reply to message #496042] Sat, 07 November 2009 15:35 Go to previous messageGo to next message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
If I am right IAction is from JFace. I would like to avoid it and use only SWT

Thanks,
Julio
Re: Set an image for a menu item [message #496189 is a reply to message #496023] Mon, 09 November 2009 10:22 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
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 #496300 is a reply to message #496189] Mon, 09 November 2009 16:08 Go to previous messageGo to next message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
Daniel Krügler wrote on Mon, 09 November 2009 05:22
antonio wrote:
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.



Thanks for reply.

Test and debug return image correctly loaded with getResourceAsStream. I put image loaded with getResourceAsStream in a label in the same window and it's correctly showed.


Thanks,
Julio
Re: Set an image for a menu item [message #496457 is a reply to message #496300] Tue, 10 November 2009 08:09 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
antonio wrote:
> Daniel Krügler wrote on Mon, 09 November 2009 05:22
>> antonio wrote:
>> 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.
>
>
> Thanks for reply.
>
> Test and debug return image correctly loaded with getResourceAsStream. I
> put image loaded with getResourceAsStream in a label in the same window
> and it's correctly showed.

OK, but in this case we need to see your code (best would be a short
snippet-like program) to check out the problem. It could also be a
problem with your actual image size or format: Have you tried a simple
16x16 .gif picture, e.g. one of those which are part of the Eclipse
installation?

HTH & 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 Go to previous messageGo to next message
antonio is currently offline antonioFriend
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 #496517 is a reply to message #496023] Tue, 10 November 2009 12:02 Go to previous messageGo to next message
Praveen  is currently offline Praveen Friend
Messages: 86
Registered: July 2009
Member
How are you creating the menuItem ? If it is created using the style
options other than Push/None (such as Radio/Check), then the image can
not be set on that menuItem, as GTK doesn't support it.
Otherwise, it seems to be a problem with resolving the image. However,
as said earlier, it is easier to debug if we have a simple snippet
showing the problem.
Re: Set an image for a menu item [message #496535 is a reply to message #496517] Tue, 10 November 2009 13:21 Go to previous messageGo to next message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
I'm using SWT.PUSH as in the snippet

Thanks,
Julio
Re: Set an image for a menu item [message #496562 is a reply to message #496510] Tue, 10 November 2009 14:38 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
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
Re: Set an image for a menu item [message #496577 is a reply to message #496562] Tue, 10 November 2009 15:48 Go to previous messageGo to next message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
nothing changed even removed it

Thanks,
Julio
Re: Set an image for a menu item [message #496903 is a reply to message #496577] Wed, 11 November 2009 16:44 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
antonio wrote:
> nothing changed even removed it

I used your snippet with the following changes:

a) I moved the line

img.dispose();

just before

display.dispose ();

b) I copied the image file you referred to somewhere
on my hard disk. Obviously the current code resulted
in a runtime-error, because the original code did not
point to that direction (This was just a blind test, I
assume, this isn't the problem you have). To fix this,
I provided an absolute path of the image location
and the program runs successfully and shows a normal
menu image as expected.

Note that I have a Windows XP SP 3. So maybe this is
a platform-specific problem?

HTH & Greetings from Bremen,

Daniel Krügler
Re: Set an image for a menu item [message #496933 is a reply to message #496903] Wed, 11 November 2009 19:14 Go to previous message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
Yes, I guess so. I'm going to open a bug ticket for SWT

Thanks,
Julio
Previous Topic:Capture a mouseHover on disable checkBox
Next Topic:Listen IProgressMonitor stop button
Goto Forum:
  


Current Time: Fri Jul 10 13:28:40 GMT 2020

Powered by FUDForum. Page generated in 0.02725 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top