user1803305
user1803305

Reputation: 31

Getting the value of a cell in a swt table

I created a swt table that has 3 columns, the first is check column. I used this code:

  table = new Table(container, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL
            | SWT.H_SCROLL|SWT.MULTI);

When I select one item, a text is created in the third colonm. the code is as below:

listener = new Listener() {
 @Override public void handleEvent(Event event) {
 if (event.detail == SWT.CHECK) {
 final TableItem current = (TableItem) event.item; 
if (current.getChecked()) { 
final TableEditor editor = new TableEditor(table); 
text = new Text(table, SWT.NONE);
 editor.grabHorizontal = true;
}

I want to get the value of the cell that matches the selected item with the third column but couldn't get it with a selectedItem.getText(2). Any help please?

Upvotes: 0

Views: 4261

Answers (1)

Baz
Baz

Reputation: 36884

Try this code sample. It will print out the text in column 3 of the selected TableItem:

public static void main(String[] args)
{
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, true));

    Table table = new Table(shell, SWT.CHECK | SWT.MULTI);
    table.setHeaderVisible(true);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for(int i = 0; i < 4; i++)
    {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Column " + i + "           ");
        column.pack();
    }

    for(int i = 0; i < 10; i++)
    {
        TableItem newItem = new TableItem(table, SWT.NONE);
        newItem.setText(1, "ITEM " + i + " TEXT1");
        newItem.setText(2, "ITEM " + i + " TEXT2");
        newItem.setText(3, "ITEM " + i + " TEXT3");
    }

    table.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            if(event.detail == SWT.CHECK)
            {
                TableItem current = (TableItem)event.item;

                if(current.getChecked())
                {
                    System.out.println(current.getText(2));
                }
            }
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

This is what it looks like:

enter image description here

Upvotes: 1

Related Questions