How to Add Text and Image Both in a SWT LABEL
Asked Answered
E

3

8

Is there any way to add Text and Image in SWT label in a single line. Once I add image, text goes off.

Eichmann answered 29/5, 2012 at 6:35 Comment(0)
S
17

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:

enter image description here

Sloop answered 29/5, 2012 at 9:8 Comment(9)
I found it, I have to set style to SWT.RIGHT_TO_LEFT :)Peptide
Avoid using CWidgets unless very necessary. For this use-case, having 2 labels side-by-side (one with the image and the other with the text) is much better.Urinary
@Urinary - do enlighten us why one should not use CLabel and use a more verbose method suggested by you. Also there was no point in down voting the answer as it sufficiently fulfils the requirement !Sloop
The CWidget are not drawn by the underlying UI toolkit (GTK, Win32...) but by SWT itself. It can easily drive to inconsistencies with the UI framework and other SWT widgets. bugs.eclipse.org/bugs/show_bug.cgi?id=501483Urinary
@Urinary The bug link you have mentioned is just a suggestion and still doesn’t say anything about why one should not use CLabel. You have also quoted the javadoc in that bug mentioning the different strategies which CLabel deploys. In its absence the developer is supposed to write that himself, which in my experience is much more error prone if not done properly! Moreover there is an added issue of platform support, which in a way is mentioned by Eric in his comment in the bug link, for instance, windows doesn’t honour certain style bits for some of the native widgets.Sloop
@Urinary Which indirectly translates to more test cases for all the supported OS. Moreover combo or label are just native components which are wrapped in thin Java code for their respective OS. So probably you may get faster response for queries like bugs.eclipse.org/bugs/show_bug.cgi?id=501482 on OS sdk specific forums.Sloop
Everything that is pure SWT drawing (custom widgets) vs plain SWT widgets requires more maintenance on SWT side and is more likely to have bugs than regular SWT widgets, which are fully backed to native. They also cost more resources than native widgets and can suffer from accessiblity issues because overall, they're not native. Best usage of SWT is to remain as native as possible. CWidgets are not, so they should be avoided in favor of native one who leverage "for free" all features of native toolkit. You're not supposed to test UI on all OS, it's the responsibilty of SWT itself.Urinary
@Urinary a) Who said accessibility can't be provided for custom widget. Check CLabel::initAccessible, in fact custom widget are much more customizable than the native ones. b) I meant the eclipse dev team when I said you are supposed to test the widget/component on all OS as new test cases will be needed (apart from huge time needed to support multiple verion of the native gui sdk). Check this for discontinued support for next version of eclipse for older version of GTK https://dev.eclipse.org/mhonarc/lists/eclipse-dev/msg10207.html.Sloop
Infact it is much easier to maintain custom widgets for various OS, again check eclipse's common repos for custom widget VS multiple repos for native widgets https://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/bundles/org.eclipse.swt/Eclipse%20SWT%20Custom%20Widgets/common/org/eclipse/swt/custom. c) Well at least we haven't faced any resource crunch on any of our devices running various versions of windows and linux.Sloop
S
0

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?

Sociology answered 2/2, 2017 at 8:41 Comment(3)
Avoid using CWidgets unless very necessary. For this use-case, having 2 labels side-by-side (one with the image and the other with the text) is much better.Urinary
@Urinary thanks for advance.....Text and Image both are display using Clabel not proper working Label..not two label use..are you understand bro...Sociology
The CWidget are not drawn by the underlying UI toolkit (GTK, Win32...) but by SWT itself. It can easily drive to inconsistencies with the UI framework and other SWT widgets. bugs.eclipse.org/bugs/show_bug.cgi?id=501483Urinary
U
0

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(...)
Urinary answered 7/2, 2017 at 17:17 Comment(2)
two label not consider...text and image both in label... not better for question relatedSociology
@BharatZala see comments on other answer about why plain widgets (even 2 labels) are to be preferred over CWidgets.Urinary

© 2022 - 2024 — McMap. All rights reserved.