Question

I have a table which has 10 rows, what I want to do is: when I click the button "modify" I want to pass the selected Table row values to respective textboxes.

I tried some codes, which is not full filling my needs,

MainTable.addListener(SWT.Selection, new Listener() {
    public void handleEvent(Event e) {
        String string = "";
        TableItem[] selection = MainTable.getSelection();
        for (int i = 0; i < selection.length; i++)
            string += selection[i] + " ";
        final String Appname=string.substring(11, string.length()-2);
        System.out.println(Appname);
    }
});

The above code is printing the Selected row values in the Console, I want to set those values into Textboxes.

How can I do this?

Was it helpful?

Solution

Here is some example code:

private static int        columns = 3;
private static List<Text> texts   = new ArrayList<>();

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(columns, false));

    Table table = new Table(shell, SWT.FULL_SELECTION);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, columns, 1));
    table.setHeaderVisible(true);

    /* Create columns */
    for (int col = 0; col < columns; col++)
    {
        new TableColumn(table, SWT.NONE).setText("Col " + col);
    }

    /* Create cells */
    for (int row = 0; row < 10; row++)
    {
        TableItem item = new TableItem(table, SWT.NONE);

        for (int col = 0; col < table.getColumnCount(); col++)
        {
            item.setText(col, "Cell " + row + " " + col);
        }
    }

    /* Pack columns */
    for (int col = 0; col < table.getColumnCount(); col++)
    {
        table.getColumn(col).pack();
    }

    /* Create the Text fields */
    for (int col = 0; col < columns; col++)
    {
        Text text = new Text(shell, SWT.BORDER);
        texts.add(text);
    }

    /* Listen for selection */
    table.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            Table table = (Table) e.widget;
            TableItem item = table.getItem(table.getSelectionIndex());

            /* Fill the texts */
            for (int col = 0; col < table.getColumnCount(); col++)
            {
                texts.get(col).setText(item.getText(col));
            }
        }
    });

    shell.pack();
    shell.open();

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

Looks like this:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top