How to add text and image as in SWT LABEL

Is there a way to add text and image to the SWT label on the same line. When I add an image, the text is turned off.

+6
source share
3 answers

No, you cannot have an image and text at the same time in Label (unless you draw it). Otherwise 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

+15
source

Click the button to open the FileDialog window and select any image to display with text on a specific label.

 import org.eclipse.swt.custom.CLabel 

A label that supports aligned text and / or image and various border styles.

I hope this answer is helpful.

visit this page: How to upload an image for viewing in RCP?

0
source

Yes, using the intermediate composition with the correct location

  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(...) 
-1
source

Source: https://habr.com/ru/post/916881/


All Articles