如何从 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
请看图片
我选择了第三列,但它返回了第一列的 TableItem
i have selected 3rd column but it returned the TableItem of first column
我之前也遇到过同样的问题,我是这样解决的:
I encountered the same problem before, and this is how I solved it:
首先,您应该制作表 SWT.FULL_SELECTION`;
First, you should make the table SWT.FULL_SELECTION`;
然后,您必须通过读取鼠标位置来获取选定的单元格(因为基本的 swt 表不提供侦听器来获取选定的单元格;选择一个项目是可能的).代码如下:
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);
}
}
}
}
});