How to Add Text and Image Both in a SWT LABEL

孤人 提交于 2019-12-01 04:37:26

No you can't have an image and text simultaneously in a Label (unless you custom draw it). Else use org.eclipse.swt.custom.CLabel:

Code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class LabelTest {
    public static void main(String[] args) 
    {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        Image image = new Image(display, "next.png");

        CLabel label = new CLabel(shell, SWT.BORDER);
        label.setImage(image);
        label.setText("This is a CLabel !!");

        shell.pack();
        shell.open();


        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        if(image != null)
        {
            image.dispose();
            image = null;
        }
        display.dispose();

    }
}

Output:

Chetan Bhagat

Button click to open FileDialog Box and selected any image to display with text on specific label.

import org.eclipse.swt.custom.CLabel    

A Label which supports aligned text and/or an image and different border styles.

I hope this answer is usefull.

please visit this page: How to load image to view in RCP?

Yes, using an intermediary composite with the right layout

    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new RowLayout(SWT.HORIZONTAL));
    Label imageLabel = new Label(composite, SWT.NONE);
    mageLabel.setImage(...);
    Label textLabel = new Label(composite, SWT.NONE);
    textLabel.setText(...)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!