Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] Table with multiple Listeners and Editors

Hi Andreas,

This kind of questions should be post to the swt newgroup (see 
http://www.eclipse.org/newsgroups/index.html).

You should check out the others SWT snippets 
(http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets), 
specially:
-place arbitrary controls in a table 
-edit a cell in a table (in place, fancy) 

The snippet you are testing hard code the EDITABLECOLUMN column to 1, I 
made a small modification to it to show how you can set the editable 
column using the event coordinates. Here is the code: 

package snippets;
/*
 * Copyright (c) 2000, 2003 IBM Corp.  All rights reserved.
 * This file is made available under the terms of the Common Public 
License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */
 
/*
 * TableEditor example snippet: edit the text of a table item (in place)
 *
 * For a list of all SWT example snippets see
 * 
http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet88  {
 
public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        final Table table = new Table(shell, SWT.FULL_SELECTION | 
SWT.HIDE_SELECTION);
        TableColumn column1 = new TableColumn(table, SWT.NONE);
        TableColumn column2 = new TableColumn(table, SWT.NONE);
        for (int i = 0; i < 10; i++) {
                TableItem item = new TableItem(table, SWT.NONE);
                item.setText(new String[] {"item " + i, "edit this 
value"});
        }
        column1.pack();
        column2.pack();
 
        final TableEditor editor = new TableEditor(table);
        //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;
        // editing the second column
//      final int EDITABLECOLUMN = 1;
 
        table.addListener (SWT.MouseDown, new Listener () {
                public void handleEvent(Event e) {
                        // Clean up any previous editor control
                        Control oldEditor = editor.getEditor();
                        if (oldEditor != null) oldEditor.dispose();
 
                        Point pt = new Point(e.x, e.y);
                        // Identify the selected row
                        TableItem item = table.getItem(pt);
                        if (item == null) return;

                        // Identify the selected column
                        final int column = item.getBounds(0).contains(pt) 
? 0 : 1;
 
                        System.out.println(column);
                        // The control that will be the editor must be a 
child of the Table
                        Text newEditor = new Text(table, SWT.NONE);
                        newEditor.setText(item.getText(column));
                        newEditor.addModifyListener(new ModifyListener() {
                                public void modifyText(ModifyEvent e) {
                                        Text text = 
(Text)editor.getEditor();
                                        editor.getItem().setText(column, 
text.getText());
                                }
                        });
                        newEditor.selectAll();
                        newEditor.setFocus();
                        editor.setEditor(newEditor, item, column);
                }
        });
        shell.setSize(300, 300);
        shell.open();
 
        while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                        display.sleep();
        }
        display.dispose();
}

}

Felipe





"Andreas De Stefani" <anddestefani@xxxxxxx> 
Sent by: platform-swt-dev-admin@xxxxxxxxxxx
07/29/2004 07:53 AM
Please respond to
platform-swt-dev


To
<platform-swt-dev@xxxxxxxxxxx>
cc

Subject
[platform-swt-dev] Table with multiple Listeners and Editors






Hello all,
 
i'm just using a simple table to display some data, i would like to make 
some fields of the table editable, for example in a Table with 5 columns, 
i would like to make the columns 3 and 4 editable.
 
I found a snipped which does the stuff for one column using a TableEditor
 
http://www.jlab.net/eclipse/doc/3.0/org/eclipse/swt/custom/TableEditor.html
 
i can't figure out, how i can do the same for multiple columns, does 
anybody know how this works?
 
I was also thinking about using a drop down list for one column, ie. it 
should allow to choose a value from a pre-defined set. This can be 
different for each row, for example:
 
i use a list of parameters which are shown in a table, each parameter 
possibly has a different type
the first row contains a boolean parameter, therefore: in cell number 2 
you can select from[true,false]
the second row contains a parameter which uses a value set: in cell number 
2 you can select from [1,2,3,5,6]
 
Does anybody know if this is possible with a simple swt table? any hints?
 
Many thanks
 
Andy

--------------------------------------------------------------------------------------------------------------------
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom
they are addressed.
If you have received this email in error please notify the
originator of the message. This footer also confirms that this
email message has been scanned for the presence of computer viruses.

Any views expressed in this message are those of the individual
sender, except where the sender specifies and with authority,
states them to be the views of The Royal College Of Surgeons in Ireland.

--------------------------------------------------------------------------------------------------------------------




Back to the top