Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.layout.FormAttachment;
- import org.eclipse.swt.layout.FormData;
- import org.eclipse.swt.layout.FormLayout;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Table;
- import org.eclipse.swt.widgets.TableColumn;
- import org.eclipse.swt.widgets.TableItem;
- public class TableHeaderButtonsTest {
- public static void main(String[] args) {
- Display display = new Display();
- String[] titles = { "", "Column 1", "Column 2" };
- Shell shell = new Shell(display);
- shell.setLayout(new FormLayout());
- final Table table = new Table(shell, SWT.CHECK | SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
- FormData data = new FormData(800, 600);
- table.setLayoutData(data);
- table.setLinesVisible(true);
- table.setHeaderVisible(true);
- for (int i = 0; i < titles.length; i++) {
- TableColumn column = new TableColumn(table, SWT.NONE);
- column.setText(titles[i]);
- if (i > 0) {
- column.setMoveable(true); // Other columns can still be moved to id 0, though :(
- }
- }
- int count = 64;
- for (int i = 0; i < count; i++) {
- TableItem item = new TableItem(table, SWT.NONE);
- item.setText(0, "");
- item.setText(1, "Lorem ipsum " + i);
- item.setText(2, "Dolor est");
- }
- for (int i = 0; i < titles.length; i++) {
- table.getColumn(i).pack();
- }
- FormData fd = new FormData();
- fd.left = new FormAttachment(table, 5, SWT.LEFT);
- fd.top = new FormAttachment(table, 5, SWT.TOP);
- Button checkbox = new Button(shell, SWT.CHECK);
- checkbox.setLayoutData(fd);
- checkbox.moveAbove(table);
- checkbox.setToolTipText("Select all items");
- checkbox.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent event) {
- if (this.getCheckedItemCount() == table.getItemCount()) {
- this.checkAllItems(false);
- }
- else {
- this.checkAllItems(true);
- }
- }
- private void checkAllItems(final boolean check) {
- for (TableItem tableItem: table.getItems()) {
- tableItem.setChecked(check);
- }
- }
- private int getCheckedItemCount() {
- int count = 0;
- for (TableItem tableItem: table.getItems()) {
- if (tableItem.getChecked()) {
- ++count;
- }
- }
- return count;
- }
- });
- FormData fd2 = new FormData();
- fd2.left = new FormAttachment(table, 25, SWT.LEFT);
- fd2.top = new FormAttachment(table, 5, SWT.TOP);
- Button checkbox2 = new Button(shell, SWT.CHECK);
- checkbox2.setLayoutData(fd2);
- checkbox2.moveAbove(table);
- checkbox2.setToolTipText("Toggle selected items");
- checkbox2.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent event) {
- for (TableItem tableItem: table.getSelection()) {
- tableItem.setChecked(!tableItem.getChecked());
- }
- }
- });
- FormData fd3 = new FormData();
- fd3.left = new FormAttachment(table, 45, SWT.LEFT);
- fd3.top = new FormAttachment(table, 5, SWT.TOP);
- Button checkbox3 = new Button(shell, SWT.CHECK);
- checkbox3.setLayoutData(fd3);
- checkbox3.moveAbove(table);
- checkbox3.setToolTipText("Toggle all items");
- checkbox3.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent event) {
- for (TableItem tableItem: table.getItems()) {
- tableItem.setChecked(!tableItem.getChecked());
- }
- }
- });
- shell.pack();
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- display.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement