How to select one cell from the SWT table

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 this row is selected and returned, and not exactly what the cell

please tell me how can I select and get the item from this cell that I select

see image enter image description here

I selected the third column, but it returned the TableItem of the first column

+6
source share
2 answers

I used to face the same problem, and that’s how I solved it:

First you have to make table SWT.FULL_SELECTION`;

Then you should get the selected cell by reading the mouse position (because the base table swt does not provide listeners to select the selected cell, select an item, maybe). 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); } } } } }); 
+6
source

I ran into a similar problem with the nebula grid and found out that you need to enable cell selection on the table object. Here is my line of code:

 tableViewer.getGrid().setCellSelectionEnabled(true); 

Perhaps you could try replacing getGrid () with getTable (). You do not need to implement a listener of choice for this.

0
source

Source: https://habr.com/ru/post/922214/


All Articles