Table: getItem(Point p) : Table « org.eclipse.swt.widgets « Java by API






Table: getItem(Point p)


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
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 MainClass {

  public static void main(String[] a) {

    final Display d = new Display();
    final Shell shell = new Shell(d);

    shell.setSize(250, 200);

    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION
        | SWT.HIDE_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    // Create five columns
    for (int i = 0; i < 5; i++) {
      TableColumn column = new TableColumn(table, SWT.CENTER);
      column.setText("Column " + (i + 1));
      column.pack();
    }
    for (int i = 0; i < 5; i++) {
      // Create the row
      new TableItem(table, SWT.NONE);
    }

    table.addMouseListener(new MouseAdapter() {
      public void mouseDown(MouseEvent event) {
        Point pt = new Point(event.x, event.y);

        TableItem item = table.getItem(pt);
        if (item != null) {
          int column = -1;
          for (int i = 0, n = table.getColumnCount(); i < n; i++) {
            Rectangle rect = item.getBounds(i);
            if (rect.contains(pt)) {
              column = i;
              System.out.println(column);
              break;
            }
          }
        }
        
      }
    });

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

           
       








Related examples in the same category

1.new Table(Composite parent, int style)
2.new Table(s, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION)
3.Table: addListener(int type, Listener lis)
4.Table: getColumnCount()
5.Table: setHeaderVisible(boolean visible)
6.Table: setLinesVisible(boolean vi)