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
                {}
            }
        });

テーブル内の任意のセルをクリックすると、その行の最初のセルのみが選択されて返され、そのセルは正確には返されません。

選択したセルからアイテムを選択して取得する方法を教えてください

画像をご覧ください ここに画像の説明を入力してください

3番目の列を選択しましたが、最初の列のTableItemが返されました

4

2 に答える 2

6

I encountered the same problem before, and this is how I solved it:</p>

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);
                    }
                }
            }
        }
    });
于 2013-01-18T11:59:05.463 に答える
0

星雲グリッドで同様の問題に直面していて、テーブル オブジェクトでセル選択を有効にする必要があることがわかりました。ここに私のコード行があります:

tableViewer.getGrid().setCellSelectionEnabled(true);

おそらく、getGrid() を getTable() に置き換えてみてください。このために選択リスナーを実装する必要はありません。

于 2017-01-05T18:17:40.593 に答える