Adds the Edit function (in place edit) to the cells in the table)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.