Saturday, March 19, 2005

ImageDescriptor problem on JFace actions?

I found what seems to be an issue with setting an jface.resource.ImageDescriptor for an jface.Action class.  I have a few standard button bar images that I was going to use in my application.  When I first set the image for the action, I thought I was doing something wrong.  I set the image originally, like this:

public ExitAction() extends Action {
 
    public ExitAction() {
        super( “E&xit@Ctrl+X“, ImageDescriptor.createFromFile( “images\\close.gif“ ) );
        setToolTipText( “Exit Application“ );
    }
}
Unfortunately, this produced a small red square on the button and on the menu item that the ExitAction was assigned to.  I checked the image thinking it was corrupt, but all was well.  After doing a little snooping, I found that you can also create an ImageDescriptor from a swt.graphics.Image.  I changed the code to look like this:

public ExitAction() extends Action {
 
    public ExitAction() {
        super( “E&xit@Ctrl+X“ );
        setToolTipText( “Exit Application“ );
        Image image = new Image( Display.getCurrent(), “images\\close.gif“ );
        setImageDescriptor( ImageDescriptor.createFromImage( image ) );
    }
}
This produces the desired results.  I am not sure why the first way of setting the image for the action did not work, but every tutorial that I have gone through and even the code in Apress' The Definitive Guide to SWT and JFace do it like I did it in the first code fragment above.

Technorati Tags: , ,

No comments: