简体   繁体   中英

How can I tell if a widget is still active and not disposed?

Method A in Class A (Non GUI)

public void add() {
   if(etc...) {
      add data to a arraylist ..... 
   }
   Class B.updateTable();
 }

The method above basically adds data to a arraylist and then updates the tableViewer in class b.

Class B - GUI Dialog

Before I call Class B.updateTable(), I would like to check and make sure that Class B's GUI is open and not disposed.

The user can use Class A(non gui) without Class B GUI being open. Because Class A builds a user selected ArrayList and Class B displays it. So they can add data without ever trying to display it.

If I run B.updateTable() currently and Class B is not open, I get a widget disposed error.

If I can add a check and make sure the widget is not disposed before I try to update the table.

Can I check the value from Class A or do I need to write a static boolean method is Class B that returns something like shell.isDisposed?

Then in Class A method I could just add the check.

  public void add() {
     if(etc...) {
        add data to a arraylist ..... 
     }
     if(!Class B.isShellDisposed()) {       
        Class B.updateTable();
     }
  }

Is this possible or even the right way to handle the error?

The issue I was having was refreshing my table viewer, while the GUI was closed. When my user would close the GUI to add more data to the arraylist via the Main Application. I had code in the add method to refresh the table viewer in the GUI. The main problem was even though the GUI was closed, the table viewer was not null. So my code was trying to refresh something that was already disposed. Knowing I do not need to refresh the table viewer when the GUI is closed. I set the table viewer to null in the Close Button code. Then checked the viewer for null in the update method.

static public void updateTableViewer() {

  if(getViewer() != null) {
     viewer.refresh();
  }
}

So now when the GUI is closed the table viewer is NULL and when it is open the table viewer is not null. This seems to work 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