简体   繁体   English

SWT 表、TableViewer、数据更改时刷新表

[英]SWT Table, TableViewer, Refreshing Table on Data Change

I am in bad need of some guided assistance.我非常需要一些指导帮助。 I have been able to work through and get to work code examples of Tables and TableViewers.我已经能够完成并开始使用 Tables 和 TableViewers 的代码示例。 But I am not finding any examples to help me with my particle setup.但是我没有找到任何示例来帮助我进行粒子设置。

Please guide me in the right direction.请指导我正确的方向。

My classes:我的课程:

AHandler.java - This allows the user to select data from a Right Mouse Click. AHandler.java - 这允许用户从鼠标右键单击中选择数据。 During the execution I am calling a singleton class to add user selected data to a arraylist.在执行过程中,我调用了一个单例类来将用户选择的数据添加到一个数组列表中。

ASelectedData.getInstance().add(tcRevision, selectedDataset);

ASelectedData.java - is singleton class where the user selections is added to a ArrayList. ASelectedData.java - 是将用户选择添加到 ArrayList 的单例类。

ABase.java - this dialog opens with a menu click. ABase.java - 此对话框通过单击菜单打开。 The dialog should display a table with the values from the ArrayList.该对话框应显示一个表,其中包含来自 ArrayList 的值。 Some buttons that preform other actions.执行其他操作的一些按钮。

The way I have it right now is the table and tableviewer is being created in the ABase.java class.我现在拥有它的方式是在 ABase.java 类中创建表和 tableviewer。 I am pulling the information for the table from.我正在从中提取表格的信息。

 viewer.setContentProvider(new ArrayContentProvider());
 viewer.setInput(AplotSelectedDataTable.getInstance().getArrayData());

This will populate the table with the correct data.这将使用正确的数据填充表。

But the ABase.java class is MODELESS.但是 ABase.java 类是无模型的。 So why that dialog is open the user can continue to pick data using the right mouse button and adding to the arraylist.那么为什么该对话框是打开的,用户可以继续使用鼠标右键选择数据并添加到数组列表中。 But the way I have the table coded the I have to reopen the window to update the data.但是我对表格进行编码的方式我必须重新打开窗口才能更新数据。

* EDIT * *** In my main GUI I have created a TableViewer. * 编辑* ***在我的主 GUI 中,我创建了一个 TableViewer。 The TableViewer creates a table that displays data from a arraylist in a separate class. TableViewer 创建一个表,在单独的类中显示来自数组列表的数据。 The problem is that the user is still able to go to the Main Application GUI ( not my Application GUI ) and select more data while my GUI is still open.问题是用户仍然可以转到主应用程序 GUI(不是我的应用程序 GUI)并在我的 GUI 仍然打开时选择更多数据。 They expect to see the new data added to the table as they select it.他们希望在选择表时看到添加到表中的新数据。

The problem is there is no way that I know of to tell the table new data has arrived so refresh the table.问题是我知道没有办法告诉表新数据已经到达所以刷新表。 The Add method that takes the data and adds it to the ArrayList is in the ASelectedData class.获取数据并将其添加到 ArrayList 的 Add 方法位于 ASelectedData 类中。 Not the class the tableviewer is created in.不是创建 tableviewer 的类。

When the Add Method is executed, I need a way to tell the table to refresh.当执行添加方法时,我需要一种方法来告诉表刷新。 The problem is the Table and the Method is in two separate classes.问题是表和方法在两个不同的类中。

Hope this makes more sense希望这更有意义

EDIT*编辑*

my main GUI with the TableViewer我的主要 GUI 与 TableViewer

public AplotBaseDialog(Shell shell, TCSession theSession) Constructor

//////////////////////////////////////////////////////////////////////////
//                         createTableViewer()                          //
//////////////////////////////////////////////////////////////////////////
private TableViewer createTableViewer(Composite parent) {
   viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
   createColumns(parent, viewer);
   Table table = viewer.getTable();
   table.setHeaderVisible(true);
   table.setLinesVisible(true);

   viewer.setContentProvider(new ArrayContentProvider());
   viewer.setInput(AplotSelectedDataTable.getInstance().getArrayData());

   // Layout the viewer
   GridData gridData = new GridData();
   gridData.verticalAlignment = GridData.FILL;
   gridData.horizontalSpan = 2;
   gridData.grabExcessHorizontalSpace = true;
   gridData.grabExcessVerticalSpace = true;
   gridData.horizontalAlignment = GridData.FILL;
   viewer.getControl().setLayoutData(gridData);
   return viewer;
 }

//////////////////////////////////////////////////////////////////////////
//                            updateTableViewer()                       //
//////////////////////////////////////////////////////////////////////////
public void updateTableViewer() {
   viewer.refresh();
}  

The code added to other class添加到其他类的代码

  AplotBaseDialog abd = new AplotBaseDialog(null, null);

  public void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
   AplotDatasetData pp = new AplotDatasetData(tcRevision, selectedDataset);
   if (!dataArrayList.contains(pp)) {
      dataArrayList.add(pp);

   }
   abd.updateTableViewer();
  }// end add()

Although your question is not complete yet, I will try to guess your problem: You have a TableViewer in a Dialog and you can edit the ModelProvider from the main GUI.虽然您的问题尚未完成,但我会尝试猜测您的问题:您在Dialog有一个TableViewer ,您可以从主 GUI 编辑ModelProvider Now you want to know how to update the table, when you add something to the model, is that correct?现在您想知道如何更新表格,当您向模型添加内容时,是否正确?

If so, you can provide a method in the dialog which can update the TableViewer in your dialog.如果是这样,您可以在对话框中提供一个方法来更新对话框中的TableViewer Something like this:像这样的东西:

public void updateTableViewer()
{
    if(viewer != null)
        viewer.refresh();
}

You can call this method from your main gui, when you add something to the ModelProvider .当您向ModelProvider添加内容时,您可以从主 gui 调用此方法。

If this is not your question, please update your original post.如果这不是您的问题,请更新您的原始帖子。

Add this bunch of code in create control it may solve your problem:在创建控件中添加这堆代码可能会解决您的问题:

ResourcesPlugin.getWorkspace().addResourceChangeListener(new IResourceChangeListener() {
    @Override
    public void resourceChanged(IResourceChangeEvent event) {
        treeViewer.refresh();
    }
});

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

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