如何重复使用SWT图像(How to reuse images in SWT)

2019-10-20 05:29发布

我有我在网格布局中显示的项目列表。 每一行有几个图像按钮。 当行数超过1000达到我越来越org.eclipse.swt.SWTError: No more handles异常。

我用Sleak.java ,发现里面已经有一些在存储的图像。 是否有可能以某种方式重新使用这些按钮?

下面是从统计Sleak

颜色:8个光标:6种字体:19张图片:1317

码:

private void looper(List<File> dicomFiles, int start, int stop, int toShow){
        for(int i=start; i<stop; i++){
            File dicomFile = dicomFiles.get(i);
            Composite rowComposite = new Composite(dicomFilecomposite, SWT.None);
            rowComposite.setLayout(configRowCompositeLayout());
            rowComposite.setLayoutData(getRowCompositeGridData());
            rowComposite.setBackground(Colors.CELL_BG_COLOR);
            Label dicomFIleLabel = new Label(rowComposite, SWT.NULL);
            dicomFIleLabel.setLayoutData(new GridData());
            dicomFIleLabel.setText(dicomFile.getName());
            dicomFIleLabel.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
            dicomFIleLabel.setFont(FONT_10);
            Label fileSizeLabel = new Label(rowComposite, SWT.None);
            fileSizeLabel.setText((dicomFile.length() / 1000)+" "+Const.KB_LABEL);
            fileSizeLabel.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
            Composite progBarLabelComposite = new Composite(rowComposite, SWT.None);
            progBarLabelComposite.setLayout(new RowLayout());
            ProgressBar progressBar = new ProgressBar(progBarLabelComposite, SWT.HORIZONTAL | SWT.SMOOTH);
            progressBar.setVisible(true);
            Label progressPercentageLabel = new Label(progBarLabelComposite, SWT.None);
            progressPercentageLabel.setText(Const.INITIAL_PROGBAR_PERCENTAGE);
            progressPercentageLabel.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
            Composite btnWrapperComposite = new Composite(rowComposite, SWT.None);
            btnWrapperComposite.setLayout(configZeroMarginLayout(true));
            GridData btnWrapperCompositeGridData = new GridData();
            btnWrapperCompositeGridData.horizontalAlignment = GridData.END;
            btnWrapperCompositeGridData.grabExcessHorizontalSpace = true;
            btnWrapperComposite.setLayoutData(btnWrapperCompositeGridData);
            Composite uploadCancelBtnComposite = new Composite(btnWrapperComposite, SWT.None);
            uploadCancelBtnComposite.setLayout(new RowLayout());
            Composite uploadBtnComposite = new Composite(uploadCancelBtnComposite, SWT.NULL);
            uploadBtnComposite.setLayout(configZeroMarginLayout(false));
            ImageButton uploadButton = new ImageButton(uploadBtnComposite, SWT.PUSH);
            uploadButton.setImage(Const.UPLOAD_BTN_BG_IMAGE);
            uploadButton.setListener(MainShell.this);
            uploadButton.setCursor(HAND_CURSOR);
            uploadButton.setToolTipText(Const.UPLOAD_LABEL);
            ImageButton cancelButton = new ImageButton(uploadCancelBtnComposite, SWT.PUSH);
            cancelButton.setImage(Const.DELETE_BTN_BG_IMAGE);
            cancelButton.setListener(MainShell.this);
            cancelButton.setCursor(HAND_CURSOR);
            cancelButton.setToolTipText(Const.DELETE_LABEL);
            FileUploadData fileUploadData = loadFileUploadData(progBarLabelComposite, dicomFilecomposite, dicomFile, progressBar, progressPercentageLabel);
            uploadButton.setData(fileUploadData);
            uploadButton.setData(IMG_BTN_DATA_KEY, Const.UPLOAD_LABEL);
            uploadButtons.add(uploadButton);
            fileUploadDatas.add(fileUploadData);
            cancelButton.setData(fileUploadData);
            cancelButton.setData(IMG_BTN_DATA_KEY, Const.DELETE_LABEL);
            loadSeparatorField(rowComposite);
        }
    }

在这两条线在哪里这个问题在我的理解中发生。

ImageButton uploadButton = new ImageButton(uploadBtnComposite, SWT.PUSH);
    ImageButton cancelButton = new ImageButton(uploadCancelBtnComposite, SWT.PUSH);

Answer 1:

在SWT你不能重用按钮但是你可以重复使用的图片。 写一个单独的类说ImageCache和缓存中的所有影像,并要求地方从该类获取的图像。 我面临着同样的问题,解决了这种方式。 还可以创建自定义的配置方法在你的UI组件和部署自己的UI组件,因为SWT不能正确处置这些资源在大多数情况下泄漏。

SWT最佳实践说,“所有的资源必须缓存”所以你的字体,颜色,图像必须被缓存且在各自的itemRenderer的正确使用。



文章来源: How to reuse images in SWT