how to select one cell from SWT table
Asked Answered
A

2

6
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

Arnst answered 6/8, 2012 at 12:13 Comment(0)
M
6

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);
                    }
                }
            }
        }
    });
Motoring answered 18/1, 2013 at 11:59 Comment(0)
A
0

I was facing a similar problem with nebula grid and found out that you have to enable cell selection on the table object. Here is my code line:

tableViewer.getGrid().setCellSelectionEnabled(true);

Perhaps it you could try to replace getGrid() by getTable(). You will not need to implement the selection listener for this.

Akela answered 5/1, 2017 at 18:17 Comment(1)
I have a Table and each line is a TableItem. I want to be able to select only a cell in the Table, not the entire line (TableItem). I don't have setCellSelectionEnabled() in Table.Unfleshly

© 2022 - 2024 — McMap. All rights reserved.