如何使用 rcp 和 swt / jface 组件通过单击按钮在标签中动态添加图像

how to add dynamically image in label with button click using rcp and swt / jface component

单击按钮打开 文件对话框 并选择要显示在特定标签上的任何图像。

我尝试在 Label 控件中使用选定的图像设置绝对路径或相对路径 但不能正常动态工作。

所以请帮忙解决我的问题。

Java SWT Load and Resize Image to View or Editor at Dynamically


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

ImageLoader class 用于从文件或流中加载和保存图像

ImageData class 是 device-independent 图像描述

SWT 的 Image class 可用于在 GUI 中显示图像

package rcp_demo.Editor;

import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;


public class ProductEditor extends EditorPart {

        public static final  String ID="rcp_demo.Editor.product";
        private Text text;
        private CLabel lbl_image_text;

        private static final String[] FILTER_NAMES = {
        "Images(*.jpg)","Images(*.jpeg)","Images(*.png)","All Files (*.*)"};

        // These filter extensions are used to filter which files are displayed.
        private static final String[] FILTER_EXTS = { "*.jpg", "*.jpeg", "*.png", "*.*"};

    public void createPartControl(final Composite parent) {

        parent.setLayout(null);
        //Layout with absolute positioning components. 

        text = new Text(parent, SWT.BORDER);
        text.setBounds(25, 57, 169, 19);

        Button btnOpen = new Button(parent, SWT.NONE);
        btnOpen.setText("open");
        btnOpen.addSelectionListener(new SelectionAdapter() {
            @Override
        public void widgetSelected(SelectionEvent e) {

            FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
            dialog.setFilterNames(FILTER_NAMES);
            dialog.setFilterExtensions(FILTER_EXTS);
            String result = dialog.open();
            if(result!=null)
               {
                   text.setText(result);
                   Image image=SWTResourceManager.getImage(result);
                   ImageData imgData = image.getImageData();
                   imgData=imgData.scaledTo(200, 200);

                   ImageLoader imageLoader = new ImageLoader();
                   imageLoader.data = new ImageData[] {imgData};
                   imageLoader.save(result, SWT.IMAGE_COPY);

                   System.out.println(imgData.width+"....."+imgData.height);
                   lbl_image_text.setBounds(25,88,imgData.width+10,imgData.height+10);
                   //Image size set to Label
                   //lbl_image_text.setBounds(25,88,image.getBounds().width+10,image.getBounds().height+10);
                   lbl_image_text.setImage(SWTResourceManager.getImage(result));
               }
        }
    });
    btnOpen.setText("open");
    lbl_image_text = new CLabel(parent, SWT.Resize);
    }
}

CLabel class 提供了一些优于标签 class 的高级功能。 这个class可以同时显示它的文字标签和图片标签。

    lbl_image_text.setText("Welcome");
    lbl_image_text.setImage(SWTResourceManager.getImage("Image Path"));