简体   繁体   English

我如何知道小部件是否仍处于活动状态并且没有被丢弃?

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

Method A in Class A (Non GUI) 类A中的方法A(非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. 上面的方法基本上将数据添加到arraylist,然后更新类b中的tableViewer。

Class B - GUI Dialog B类-GUI对话框

Before I call Class B.updateTable(), I would like to check and make sure that Class B's GUI is open and not disposed. 在调用Class B.updateTable()之前,我想检查并确保Class B的GUI已打开并且未处置。

The user can use Class A(non gui) without Class B GUI being open. 用户可以在不打开B类GUI的情况下使用A类(非gui)。 Because Class A builds a user selected ArrayList and Class B displays it. 由于类A构建了用户选择的ArrayList,因此类B显示了它。 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. 如果我当前运行B.updateTable()并且B类未打开,则会收到小部件处理错误。

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? 我可以检查类A的值还是需要编写一个静态布尔方法(类B)返回类似于shell.isDisposed之类的方法?

Then in Class A method I could just add the check. 然后在A类方法中,我可以只添加检查。

  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. 关闭GUI时,我遇到的问题是刷新我的表查看器。 When my user would close the GUI to add more data to the arraylist via the Main Application. 当我的用户关闭GUI通过主应用程序将更多数据添加到arraylist时。 I had code in the add method to refresh the table viewer in the GUI. 我在add方法中有代码来刷新GUI中的表查看器。 The main problem was even though the GUI was closed, the table viewer was not null. 主要问题是即使关闭了GUI,表查看器也不为空。 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. 知道我不需要在GUI关闭时刷新表查看器。 I set the table viewer to null in the Close Button code. 我在关闭按钮代码中将表查看器设置为null。 Then checked the viewer for null in the update method. 然后在更新方法中检查查看器是否为null。

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. 因此,现在在关闭GUI时,表查看器为NULL,而在打开时,表查看器不为null。 This seems to work for me. 这似乎为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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