Advertisement
Guest User

SWT table with buttons in column header

a guest
Jan 21st, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. package test;
  2.  
  3. import org.eclipse.swt.SWT;
  4. import org.eclipse.swt.events.SelectionAdapter;
  5. import org.eclipse.swt.events.SelectionEvent;
  6. import org.eclipse.swt.layout.FormAttachment;
  7. import org.eclipse.swt.layout.FormData;
  8. import org.eclipse.swt.layout.FormLayout;
  9. import org.eclipse.swt.widgets.Button;
  10. import org.eclipse.swt.widgets.Display;
  11. import org.eclipse.swt.widgets.Shell;
  12. import org.eclipse.swt.widgets.Table;
  13. import org.eclipse.swt.widgets.TableColumn;
  14. import org.eclipse.swt.widgets.TableItem;
  15.  
  16. public class TableHeaderButtonsTest {
  17.  
  18.    public static void main(String[] args) {
  19.       Display display = new Display();
  20.       String[] titles = { "", "Column 1", "Column 2" };
  21.       Shell shell = new Shell(display);
  22.       shell.setLayout(new FormLayout());
  23.       final Table table = new Table(shell, SWT.CHECK | SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
  24.       FormData data = new FormData(800, 600);
  25.       table.setLayoutData(data);
  26.       table.setLinesVisible(true);
  27.       table.setHeaderVisible(true);
  28.       for (int i = 0; i < titles.length; i++) {
  29.          TableColumn column = new TableColumn(table, SWT.NONE);
  30.          column.setText(titles[i]);
  31.          if (i > 0) {
  32.             column.setMoveable(true); // Other columns can still be moved to id 0, though :(
  33.          }
  34.       }
  35.       int count = 64;
  36.       for (int i = 0; i < count; i++) {
  37.          TableItem item = new TableItem(table, SWT.NONE);
  38.          item.setText(0, "");
  39.          item.setText(1, "Lorem ipsum " + i);
  40.          item.setText(2, "Dolor est");
  41.       }
  42.       for (int i = 0; i < titles.length; i++) {
  43.          table.getColumn(i).pack();
  44.       }
  45.  
  46.  
  47.       FormData fd = new FormData();
  48.       fd.left = new FormAttachment(table, 5, SWT.LEFT);
  49.       fd.top = new FormAttachment(table, 5, SWT.TOP);
  50.  
  51.       Button checkbox = new Button(shell, SWT.CHECK);
  52.       checkbox.setLayoutData(fd);
  53.       checkbox.moveAbove(table);
  54.       checkbox.setToolTipText("Select all items");
  55.       checkbox.addSelectionListener(new SelectionAdapter() {
  56.          @Override
  57.          public void widgetSelected(SelectionEvent event) {
  58.             if (this.getCheckedItemCount() == table.getItemCount()) {
  59.                this.checkAllItems(false);
  60.             }
  61.             else {
  62.                this.checkAllItems(true);
  63.             }
  64.          }
  65.  
  66.          private void checkAllItems(final boolean check) {
  67.             for (TableItem tableItem: table.getItems()) {
  68.                tableItem.setChecked(check);
  69.             }
  70.          }
  71.  
  72.          private int getCheckedItemCount() {
  73.             int count = 0;
  74.  
  75.             for (TableItem tableItem: table.getItems()) {
  76.                if (tableItem.getChecked()) {
  77.                   ++count;
  78.                }
  79.             }
  80.  
  81.             return count;
  82.          }
  83.       });
  84.  
  85.  
  86.       FormData fd2 = new FormData();
  87.       fd2.left = new FormAttachment(table, 25, SWT.LEFT);
  88.       fd2.top = new FormAttachment(table, 5, SWT.TOP);
  89.  
  90.       Button checkbox2 = new Button(shell, SWT.CHECK);
  91.       checkbox2.setLayoutData(fd2);
  92.       checkbox2.moveAbove(table);
  93.       checkbox2.setToolTipText("Toggle selected items");
  94.       checkbox2.addSelectionListener(new SelectionAdapter() {
  95.          @Override
  96.          public void widgetSelected(SelectionEvent event) {
  97.             for (TableItem tableItem: table.getSelection()) {
  98.                tableItem.setChecked(!tableItem.getChecked());
  99.             }
  100.          }
  101.       });
  102.  
  103.       FormData fd3 = new FormData();
  104.       fd3.left = new FormAttachment(table, 45, SWT.LEFT);
  105.       fd3.top = new FormAttachment(table, 5, SWT.TOP);
  106.  
  107.       Button checkbox3 = new Button(shell, SWT.CHECK);
  108.       checkbox3.setLayoutData(fd3);
  109.       checkbox3.moveAbove(table);
  110.       checkbox3.setToolTipText("Toggle all items");
  111.       checkbox3.addSelectionListener(new SelectionAdapter() {
  112.          @Override
  113.          public void widgetSelected(SelectionEvent event) {
  114.             for (TableItem tableItem: table.getItems()) {
  115.                tableItem.setChecked(!tableItem.getChecked());
  116.             }
  117.          }
  118.       });
  119.  
  120.       shell.pack();
  121.       shell.open();
  122.       while (!shell.isDisposed()) {
  123.          if (!display.readAndDispatch())
  124.             display.sleep();
  125.       }
  126.       display.dispose();
  127.    }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement