简体   繁体   中英

How can i solve the “Widget is disposed” Problem?

In my view i create buttons in a loop and for each button i assign an image, i initialize the two images in the constructor of the view.

here the code for the buttons:

for (int i = 0; i < 5; i++) {
        final OButton button = new OButton(compositeT1, SWT.PUSH);
        button.setText("T1" + i);
        button.setData("1"+i);
        GridData gd = new GridData(50,30);
        button.setButtonRenderer(RedButtonRenderer.getInstance());
        button.setLayoutData(gd);
         if(!tournees.contains(button.getData().toString())){
             button.setToolTipText("Livraison");
             button.setImage(imgCamion);
         }else{
             button.setToolTipText("Appel");
             button.setImage(imgCasque);

         }
        button.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int tourneeInt = Integer.parseInt(button.getData().toString());
                if (button.getImage().equals(imgCasque)) {
                    openAppelEditor(button.getData().toString());
                }else if(tourneeInt == 12 || tourneeInt == 13  || tourneeInt == 14){
                    openLivraisonEditor(String.valueOf(tourneeInt - 2),button.getData().toString());
                }else if(tourneeInt == 10) {
                    openLivraisonEditor("43",button.getData().toString());
                }else if (tourneeInt == 11 ){
                    openLivraisonEditor("44",button.getData().toString());
                }


            }
            @Override
                public void widgetDefaultSelected(SelectionEvent e) {}
            }); 

i also add the function dispose():

@Override
public void dispose() {
    super.dispose();
    this.imgCamion.dispose();
    this.imgCasque.dispose();
}

The problem is when i close the view and reopen it i receive the error: "Widget is disposed":

here is the error lines:

.MESSAGE Unable to create view ID....:/SelectionTourneePortefeuilleView: Widget is disposed
!STACK 0

org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(SWT.java:4361)
at org.eclipse.swt.SWT.error(SWT.java:4276)
at org.eclipse.swt.SWT.error(SWT.java:4247)
at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:340)
at org.mihalis.opal.obutton.OButton.getImage(Unknown Source)
at org.mihalis.opal.obutton.AbstractButtonRenderer.createDisabledImage(Unknown Source)
at org.mihalis.opal.obutton.OButton.setImage(Unknown Source)
at com.croquegel.crm.vente.gestionportefeuille.view.SelectionTourneePortefeuilleView.createButtons(SelectionTourneePortefeuilleView.java:582)
at com.croquegel.crm.vente.gestionportefeuille.view.SelectionTourneePortefeuilleView.createPartControl(SelectionTourneePortefeuilleView.java:347)
at org.eclipse.ui.internal.ViewReference.createPartHelper(ViewReference.java:386)
at org.eclipse.ui.internal.ViewReference.createPart(ViewReference.java:240)
at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:595)
at org.eclipse.ui.internal.PartPane.setVisible(PartPane.java:315)
at org.eclipse.ui.internal.ViewPane.setVisible(ViewPane.java:534)
at org.eclipse.ui.internal.presentations.PresentablePart.setVisible(PresentablePart.java:180)
at org.eclipse.ui.internal.presentations.util.PresentablePartFolder.select(PresentablePartFolder.java:270)
at org.eclipse.ui.internal.presentations.util.LeftToRightTabOrder.select(LeftToRightTabOrder.java:65)
at org.eclipse.ui.internal.presentations.util.TabbedStackPresentation.selectPart(TabbedStackPresentation.java:473)
at org.eclipse.ui.internal.PartStack.refreshPresentationSelection(PartStack.java:1245)
at org.eclipse.ui.internal.PartStack.setSelection(PartStack.java:1198)
at org.eclipse.ui.internal.PartStack.showPart(PartStack.java:1597)
at org.eclipse.ui.internal.PartStack.createControl(PartStack.java:643)
at org.eclipse.ui.internal.PartStack.createControl(PartStack.java:570)
at org.eclipse.ui.internal.PartSashContainer.createControl(PartSashContainer.java:568)
at org.eclipse.ui.internal.PerspectiveHelper.activate(PerspectiveHelper.java:272)
at org.eclipse.ui.internal.Perspective.onActivate(Perspective.java:981)
at org.eclipse.ui.internal.WorkbenchPage.setPerspective(WorkbenchPage.java:3726)
at org.eclipse.ui.internal.WorkbenchPage.busySetPerspective(WorkbenchPage.java:1124)
at org.eclipse.ui.internal.WorkbenchPage.access$16(WorkbenchPage.java:1108)
at org.eclipse.ui.internal.WorkbenchPage$19.run(WorkbenchPage.java:3827)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchPage.setPerspective(WorkbenchPage.java:3825)
at org.eclipse.ui.internal.Workbench.showPerspective(Workbench.java:2875)

How can i know which widget is disposed?

i debugged after reopening the view the attribut disposed of the image is false.

and since i'm working on an RCP application i add the view in the corresponding perspective.

The solution was to assign the image to the button in a thread.

Display.getDefault().asyncExec(new Runnable() {
                    @Override
                        public void run() {
                        //yourCode
                           });

Worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM