Refreshing a table - when clearing an array of table data

Data Model Class - Datamodel

ArrayList = dataArray (saves data for tableviewer) clearArray () - method that clears the array

Table Viewer Class - DataTableViewer

The last column of the table displays the button

Dialog class - DataDialog

Create table viewer:

viewer.setInput(DataModel.getInstance().getArrayData());

      

Dialog Button = Clear Results:

DataModel.getInstance().clearArray();
viewer.refresh();

      

Question

When the user clicks the "Clear Results" button. It removes the displayed data from the table. But the buttons are still displayed in the table.

I know the problem is that the clear button is clearing the dataArray in the DataModel. Buttons do not receive notification that data is now not passed.

How do I update the buttons, so when the user clears the table, the buttons are cleared as well?

* EDIT **

I haven't included all the code, but I tried to show the basics of my problem. The class where I create an arraylist of data to display in the table.

public class DataModel  {

   static ArrayList data = new ArrayList();

   private DataModel() {
   }

   public void buildArray(String id, String name) {
      data.add(id, name)
   }

   public void clearArray() {
      data.clear();
   }    

   public ArrayList getDataArray() {
       return data;
   }   
 }    

      

This is the table view class

public class DataTableViewer extends TableViewer {   

public DataTableViewer() {
   Table table = getTable();
   createColumns();
   table.setHeaderVisible(true);
   table.setLinesVisible(true);
   setContentProvider(new ArrayContentProvider());
}

private void createColumns() {
   TableViewerColumn col = new TableViewerColumn(this , SWT.NONE);
   col.getColumn().setWidth(150);
   col.getColumn().setText("ID");
   col.setLabelProvider(new ColorColumnLabelProvider());

   col = new TableViewerColumn(this , SWT.NONE);
   col.getColumn().setWidth(150);
   col.getColumn().setText("Name");
   col.setLabelProvider(new ColorColumnLabelProvider());

   col = new TableViewerColumn(this , SWT.NONE);
   col.getColumn().setWidth(50);
   col.getColumn().setText("");
   col.setLabelProvider(new ColorColumnLabelProvider());
}

public class ColorColumnLabelProvider extends ColumnLabelProvider {
   @Override
   public void update(final ViewerCell cell) {
      Object element = cell.getElement();
      if(element instanceof DataModel.SaveData) {
        DataModel.SaveData p = (DataModel.SaveData) element;
        switch(cell.getColumnIndex()) {            
           case 0: {
              cell.setText(p.getID());
              break;
           }
           case 1: {
              cell.setText(p.getName());
              break;
           }
           case 2: {
              Map<Object, Button> buttons = new HashMap<Object, Button>();   
              TableItem item = (TableItem) cell.getItem();
              Button button;
              String filename = (((DataModel.SaveData) element).getID() + "/" + ((DataModel.SaveData) element).getName());
              if(buttons.containsKey(cell.getElement())) {
                 button = buttons.get(cell.getElement());
              }
              else
              {
                button = new Button((Composite) cell.getViewerRow().getControl(),SWT.PUSH);
                button.setImage(appReg.getImage("ICON"));
                button.setData("file.id", filename);
                buttons.put(cell.getElement(), button);
              }
              TableEditor editor = new TableEditor(item.getParent());
              editor.grabHorizontal  = true;
              editor.grabVertical = true;
              editor.setEditor(button , item, cell.getColumnIndex());
              button.addListener(SWT.Selection, new SelectionListener());
              editor.layout();
              break;
           }

class SelectionListener implements Listener {

  public SelectionListener() {
  }

  @Override
  public void handleEvent(Event event) {
    if (event.widget instanceof Button) {
      String fileId =  (String) event.widget.getData("file.id");
      final File viewerFile = new File(fileId);
      try {
        Desktop.getDesktop().open(viewerFile);
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  }// End SelectionListener Class
} 

      

This is a dialog class that displays a tableviewer built from an arraylist from the DataModel class and using a table from the DataTableViewer class.

 public class DataDialog extends TitleAreaDialog {

   Button clearButton;
   DataTableViewer  viewer;

    public DataDialog() {
    }

    protected Control createDialogArea() {
        createClearResultsButton();
        createTableViewer();
}

private void createTableViewer() {
    viewer = new DataTableViewer(parent, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION | SWT.MULTI);
        viewer.setInput(DataModel.getInstance().getDataArray());
}

protected void createClearResultsButton() {
    clearButton = new Button(composite, SWT.PUSH);
        clearButton.setText("Clear Results");
        clearButton.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
             boolean more = MessageDialog.openConfirm(null, "Confirmation Message", "Are you sure you want to remove all results from the table?");
           if(more == true) {
              clearTableRows(); 
           }
        }
    });
}

public void clearTableRows() { 
     DataModel.getInstance().clearDataArray();
     viewer.refresh();
 }
}   

      

The main question:
1. Class DialogData - opens

  • The user sees 2 rows in the table with a button for each row in the third column.

  • Each button corresponds to specific data on their line (possibly a filename)

  • The user has finished viewing the data in the table.

  • User clicks clear button to delete data

  • The Clicked method clears the ArrayList data in the DataModel class. The code then updates the viewer.

  • The user now sees an empty table, but the buttons are still visible.

I am trying to figure out how to get rid of the buttons or how to clear the Button map in the DataTableViewer class when the rest of the data is cleared.

I think when the data is clear these are not reset buttons. This way, the buttons that have already been created are still valid and saved on the Map.

Hope it understands more.

+3


source to share


1 answer


With the method, viewer.getTable().getChildren();

you can access all the child elements Table

.

You can iterate over all the children Table

and manually dispose Button

.

Control[] children = viewer.getTable().getChildren();
for(Control element : children) {
    if(element instanceof Button) {
        element.dispose();
    }
}

      

I couldn't get your example to work, so I hope it works for your case. You probably need to insert your code into your methodclearTableRows();



Something like that:

public void clearTableRows() {
    DataModel.getInstance().clearDataArray();

    Control[] children = viewer.getTable().getChildren();
    for (Control element : children) {
        if (element instanceof Button) {
            element.dispose();
        }
    }
    viewer.refresh();
}

      

Source:

+1


source







All Articles