如何从 SWT 表中选择一个单元格 [英] how to select one cell from SWT table

查看:33
本文介绍了如何从 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);
                    }
                }
            }
        }
    });

这篇关于如何从 SWT 表中选择一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆