Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] Problem while removing SWT Controls from SWT Table (Style : SWT.CHECK)

Hi, Srirekha.

You need to save the table editors somewhere (probably saving them in the table item data is the easiest), and then dispose of the table editors and the editor controls before removing the table item.
Then you need the table's parent to resize and redraw.
This snippet will hopefully show you what you need to do:

package snippets;

import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TableCellEditorComboTextButton {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.CHECK);
    table.setLinesVisible(true);
    for (int i = 0; i < 3; i++) {
      TableColumn column = new TableColumn(table, SWT.NONE);
      column.setWidth(100);
    }
    for (int i = 0; i < 12; i++) {
      new TableItem(table, SWT.NONE);
    }
    TableItem[] items = table.getItems();
    for (int i = 0; i < items.length; i++) {
      TableItem item = items[i];
      TableEditor editor = new TableEditor(table);
      CCombo combo = new CCombo(table, SWT.NONE);
      combo.setText("Combo " + i);
      combo.add("item 1");
      combo.add("item 2");
      editor.grabHorizontal = true;
      editor.setEditor(combo, item, 0);
      item.setData("ComboEditor", editor);
      editor = new TableEditor(table);
      Text text = new Text(table, SWT.NONE);
      text.setText("Text " + i);
      editor.grabHorizontal = true;
      editor.setEditor(text, item, 1);
      item.setData("TextEditor", editor);
      editor = new TableEditor(table);
      Button button = new Button(table, SWT.CHECK);
      button.setText("" + i);
      button.pack();
      editor.minimumWidth = button.getSize().x;
      editor.horizontalAlignment = SWT.LEFT;
      editor.setEditor(button, item, 2);
      item.setData("ButtonEditor", editor);
    }
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Remove checked items");
    button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                    TableItem[] items = table.getItems();
                    for (TableItem item : items) {
                            if(item.getChecked()){  
                                    TableEditor editor = (TableEditor) item.getData("ComboEditor");
                                    editor.getEditor().dispose();
                                    editor.dispose();
                                    editor = (TableEditor) item.getData("TextEditor");
                                    editor.getEditor().dispose();
                                    editor.dispose();
                                    editor = (TableEditor) item.getData("ButtonEditor");
                                    editor.getEditor().dispose();
                                    editor.dispose();
                                    table.remove(table.indexOf(item));
                                shell.pack();
                            }
                    }        

            }
        });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}



From: "Srirekha P. K." <srirekha.pk@xxxxxxxxxxxxx>
To: <platform-swt-dev@xxxxxxxxxxx>,
Date: 11/20/2012 12:45 AM
Subject: [platform-swt-dev] Problem while removing SWT Controls from SWT        Table (Style : SWT.CHECK)
Sent by: platform-swt-dev-bounces@xxxxxxxxxxx





 
Hi All,
 
I have created an SWT table (style SWT.CHECK) following the SWT code snippet in link below:
 
http://www.java2s.com/Tutorial/Java/0280__SWT/TableCellEditorComboTextandButton.htm
<http://www.java2s.com/Tutorial/Java/0280__SWT/TableCellEditorComboTextandButton.htm>  
 
As in snippet I have created three rows in table, first row contains SWT Combo, the other two contains SWT text boxes
 
My requirement is that I need to remove the selected (checked)items from table. But this could not be achieved. When I try to remove the selected item only the check box is being removed, other items in that row (combo and text boxes) remains there.
 
The code used to remove the item is as follows,
 
  TableItem[] items = table.getItems();
  for (TableItem item : items) {
   if(item.getChecked()){                          
       table.remove(table.indexOf(item));
   }
  }        
     
Is it because we are placing SWT controls as table item?
 
1. Please suggest a way to remove the table items with SWT Controls in it...
2. Also please let me know how I can access individual controls (Combo box and values in it)
 
 
Regards,
Srirekha
 

 
***** Confidentiality Statement/Disclaimer *****

This message and any attachments is intended for the sole use of the intended recipient. It may contain confidential information. Any unauthorized use, dissemination or modification is strictly prohibited. If you are not the intended recipient, please notify the sender immediately then delete it from all your systems, and do not copy, use or print. Internet communications are not secure and it is the responsibility of the recipient to make sure that it is virus/malicious code exempt.
The company/sender cannot be responsible for any unauthorized alterations or modifications made to the contents. If you require any form of confirmation of the contents, please contact the company/sender. The company/sender is not liable for any errors or omissions in the content of this message.
_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/platform-swt-dev



Back to the top