Mar 19 2005

ImageDescriptor problem on JFace actions?

Published by Kerry at 7:46 pm under Uncategorized

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: , ,

One Response to “ImageDescriptor problem on JFace actions?”

  1. Jeremyon 19 Mar 2008 at 7:43 am

    Your problem with the button could potentially be related to when you are creating your ExitAction… Obviously I don’t have the whole picture but I’ll vernture a guess anyways. In JFace the Image Descriptor requires the Display to be around. So if you for example called the new ExitAction from the constructor of a JFace Application Window. You would have the Display linked up properly. Because that is when the Shell is created and attached to the Display.

    Try moving the code to the configureShell method of the ApplicationWindow. Here you will have the display / shell wired up correctly and it will be just before the shell is attached to the window which is where you want to be. I’ve tested on my end and it works for me if I put it into that function.

Trackback URI | Comments RSS

Leave a Reply