With pure SWT, You can implement the in place edit function in cells,CodeSee this example. Here we will talk about using jface to do similar work: When you click a cell, a drop-down menu appears. You can select to change the attributes of the row object represented by the cell.
The introduction to org. Eclipse. jface. Viewers. tableviewer. tableviewer is not described here. We generally define tableviewer as follows:
Tableviewer viewer =NewTableviewer (...); viewer. gettable (). setheadervisible (True); Viewer. setcontentprovider (...); viewer. setlabelprovider (...);//Add table columnsTablecolumn column =NewTablecolumn (viewer. gettable (), SWT. None); column. settext ("...");
To add the editing function, you must first define an org. eclipse. jface. viewers. the celleditor array corresponds to each column of tableviewer. In this example, the second column (column 1) needs to be edited in a drop-down box, so you can define this celleditor array.
FinalCelleditor [] editors =NewCelleditor [tvore. gettable (). getcolumncount ()]; editors [1] =NewComboboxcelleditor (viewer. gettable (),NewString [] {}, SWT. read_only); viewer. setcelleditors (editors); viewer. setcolumnproperties (columnnames );
Then define the cellmodifier (Org. eclipse. jface. viewers. icellmodifier), this interface defines three methods, canmodify () indicates whether the cell can be edited; getvalue () should return the current value of the cell, because we use the drop-down box, therefore, an integer is returned to indicate the selected index. In the modify () method, we reflect the value modified by the user to the actual model. The Code is as follows. Note that iore is part of our model in the Code. Each row in the table is an iore object, iore. getorderparams () gets the options in the drop-down box. The options of each row are related to the iore object represented by that row:
Viewer. setcellmodifier ( New Icellmodifier (){ Public Boolean Canmodify (object element, string property) {iore = (Iore) element; string [] items = New String [ore. getorderparams (). Size ()]; For ( Int I = 0; I <ore. getorderparams (). Size (); I ++ ) {Namevaluepair pair = (Namevaluepair) ore. getorderparams (). Get (I); items [I] =Pair. getvalue ();} editors [ 1] = New Comboboxcelleditor (viewer. gettable (), items, SWT. read_only ); Return Property. Equals (columnnames [1 ]);} Public Object getvalue (object element, string property ){ If (Property. Equals (columnnames [1 ]) {Iore Ore = (Iore) element; For (Int I = 0; I <ore. getorderparams (). Size (); I ++ ) {Namevaluepair pair = (Namevaluepair) ore. getorderparams (). Get (I ); If (Pair. getvalue (). Equals (ore. getorderparamvalue ())) Return New INTEGER (I );} Return New INTEGER (0 );} Return Null ;} Public Void Modify (object element, string property, object Value) {tableitem item = (Tableitem) element; iore Ore = (Iore) item. getdata (); namevaluepair pair = (Namevaluepair) ore. getorderparams (). get (integer) value ). intvalue (); ore. setorderparamvalue (pair. getvalue (); viewer. refresh ();}});
Next ...... Oh, it's so easy! Do not believe it? Please refer to the following running results.