Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select one cell from SWT table

Tags:

java

swt

table.addSelectionListener(new SelectionAdapter() 
        {
            public void widgetSelected(SelectionEvent e) 
            {
                if(table.getSelectionIndex() != -1)
                {
                    System.out.println(table.getSelectionIndex());
                    TableItem item = table.getItem(table.getSelectionIndex());
                    System.out.println(item.toString());
                }
                else
                {}
            }
        });

when i click on any cell in my table, only the first cell of that row is selected and returned and not exactly that cell

please tell me how can i select and get item from exactly that cell which i select

please see the image enter image description here

i have selected 3rd column but it returned the TableItem of first column

like image 700
Asad Ullah Avatar asked Aug 06 '12 12:08

Asad Ullah


1 Answers

I encountered the same problem before, and this is how I solved it:

First, you should make the table SWT.FULL_SELECTION`;

Then, you have to get the selected cell by reading the mouse position (because the basic swt table does not provide listeners to get selected cell; select a item is possible). Here is the code:

    table.addListener(SWT.MouseDown, new Listener(){
        public void handleEvent(Event event){
            Point pt = new Point(event.x, event.y);
            TableItem item = table.getItem(pt);
            if(item != null) {
                for (int col = 0; col < table.getColumnCount(); col++) {
                    Rectangle rect = item.getBounds(col);
                    if (rect.contains(pt)) {
                        System.out.println("item clicked.");
                        System.out.println("column is " + col);
                    }
                }
            }
        }
    });
like image 179
cindy Avatar answered Oct 16 '22 07:10

cindy