Benoit Goderre Benoit Goderre - 1 year ago 138
Java Question

How to create an image in Java SWT without display

I am learning SWT with the goal to create a wizard. I am modifying an existing tutorial. At this point, I am trying to display an image in one page of the wizard:

class FrontPage extends WizardPage {

FrontPage() {
setTitle("Listes des joueurs");
setDescription("Veuillez déplacer toutes les listes dans le même fichier (ex: C:\\Badminton\\)");

public void createControl(Composite parent) {
Composite composite = new Composite(parent, SWT.NULL);
GridLayout gridLayout = new GridLayout(2, false);

Canvas canvas = new Canvas(composite, SWT.NONE);
canvas.setBounds(10, 10, 693, 253);

canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
Image image = new Image(null, "C:\\Benoit\\Work\\Java\\Badminton\\Folder_List_Players.png");

e.gc.drawImage(image, 10, 10);





When running this code, my image, which is roughly 600 by 200, is displayed as a thumbnail (10x10). I would like to display it full size.

  1. I understand that I might have a Display object in the Image constructor, but I am unsure how to make it work with the Composite parent object. It is interesting however that I am still able to display the image with a null Display object.

  2. The canvas.setBounds(10, 10, 693, 253); does not seem to have any impact.

Thanks in advance for any hints or help!!!

Answer Source

If an Image is created with a null Display, the current Display is used. For clarity, you should always provide a Display when creating images.

And there is no shortage of references to the current display either, for example:

Display display = event.display;


Display display = parent.getDisplay();

In order to display an image, prefer using a Label like this:

Label imageLabel = new Label( parent, SWT.NONE );
imageLabel.setImage( ... );

To layout controls, stay away from using setBounds() and absolute coordinates. Use layout managers instead:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download