简体   繁体   中英

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. 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. 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.

ABase.java - this dialog opens with a menu click. The dialog should display a table with the values from the 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. 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. 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. The TableViewer creates a table that displays data from a arraylist in a separate class. 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. 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. Not the class the tableviewer is created in.

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

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. 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. 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 .

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();
    }
});

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