Palani
Palani

Reputation: 1921

Editable SWT table

How to edit SWT table Values without Using Mouse Listeners?

Upvotes: 3

Views: 12317

Answers (5)

Milen Grigorov
Milen Grigorov

Reputation: 352

I suggest you to us TableViewer, it is very powerful table which it you can use databinding very easy too.

Upvotes: 0

Andre
Andre

Reputation: 162

You can get or set the value of a item, for example:

Table table = new Table(parent, SWT.NONE);
TableItem item = new TableItem(table, SWT.NONE);
item.setText("My new Text");

Upvotes: 0

Tonny Madsen
Tonny Madsen

Reputation: 12718

If you can use JFace as well and not just pain SWT, have a look at the JFace Snippets, especially

Upvotes: 1

Dinup Kandel
Dinup Kandel

Reputation: 2505

    final int EDITABLECOLUMN = 1;
tblProvisionInfo.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // Clean up any previous editor control
            final TableEditor editor = new TableEditor(tblProvisionInfo);               
            // The editor must have the same size as the cell and must
            // not be any smaller than 50 pixels.
            editor.horizontalAlignment = SWT.LEFT;
            editor.grabHorizontal = true;
            editor.minimumWidth = 50;
            Control oldEditor = editor.getEditor();
            if (oldEditor != null)
                oldEditor.dispose();                

            // Identify the selected row
            TableItem item = (TableItem) e.item;
            if (item == null)
                return;

            // The control that will be the editor must be a child of the
            // Table
            Text newEditor = new Text(tblProvisionInfo, SWT.NONE);
            newEditor.setText(item.getText(EDITABLECOLUMN));

            newEditor.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent me) {
                    Text text = (Text) editor.getEditor();
                    editor.getItem()
                            .setText(EDITABLECOLUMN, text.getText());
                }
            });         
            newEditor.selectAll();
            newEditor.setFocus();           
            editor.setEditor(newEditor, item, EDITABLECOLUMN);      

        }
    });     

Here tblProvision is the name of your table. you can just now edit Your table by clicking on it. I have Declare EDITABLECOLUMN. this is the column that u want to edit.

Upvotes: 6

mecsco
mecsco

Reputation: 2250

Do the TableEditor snippets in the below link help?

SWT Snippets

The first example in the TableEditor section uses a SelectionListener on the table (unlike the second example which uses a MouseDown event you mentioned you don't want)

You could perhaps make use of the TraverseListener or KeyListener too to help you achieve what you want.

Upvotes: 7

Related Questions