本文介绍了如何在 SWT 中重用图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以网格布局显示的项目列表.每行都有几个图像按钮.当行数超过 1000 时,我收到 org.eclipse.swt.SWTError: No more handles 异常.

I have a list of items which I display in a grid layout. Each row have couple of image buttons. When the row count reached over 1000 I am getting org.eclipse.swt.SWTError: No more handles exception.

我用了Sleak.java,发现内存中出现了多张图片.是否可以以某种方式重复使用这些按钮?

I used Sleak.java and found that there have been a number of images in the memory. Is it possible to reuse these buttons somehow?

以下是Sleak的统计数据.

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

Colors: 8Cursors: 6Fonts: 19Images: 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);
        }
    }

在这两行中是我理解的问题所在.

In these two lines are where the issue is occurring in my understanding.

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

推荐答案

在 SWT 中,您不能重复使用按钮,但可以重复使用图像.编写一个单例类 ImageCache 并缓存其中的所有图像,并在需要的地方从该类中获取图像.我遇到了同样的问题并以这种方式解决了它.还要在您的 UI 组件中创建您的自定义处置方法并自己处置 UI 组件,因为 SWT 不会正确处置它们并且在大多数情况下会出现资源泄漏.

In SWT you can not reuse buttons however you can reuse images. Write a singleton class say ImageCache and cache all the images in it and fetch images from this class wherever required.I faced the same problem and solved it this way.Also create your custom dispose methods in your UI components and dispose UI components yourself because SWT does not disposes them properly and resources leak in most cases.

SWT 最佳实践说必须缓存所有资源",因此您的字体、颜色、图像必须缓存并在相应的 ItemRenderer 中正确使用.

SWT best practice says that "all resources must be cached" so your fonts, colors, images must be cached and used properly in respective ItemRenderers.

这篇关于如何在 SWT 中重用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 02:58
查看更多