// Originally taken from "The Definitive Guide to Swt and JFACE"
Table viewers
SWT's form widget, the last article details its usage. You become a Table, then generate one
TableItem is placed in the form. You will directly manage each line, each column, you need to personally
According to sorting, you will find it very troublesome.
JFACE's TableViewer solves all questions. I like other viewers, you are a Tableviwer,
Set ContentProvider, set LabelProvider, and finally set Input. The following table shows TableViewer
The construction method, two methods that do not use TABLE to create a table.
Constructor Description
TableViewer (Composite Parent) Generates a TableViewer for PARENT
TableViewer (Composite Parent, int Style) generates a specific style for Parent
TableViewer
TableViewer (Table Table) Generates TableViewer for a specific table
***** Using A TableViewer
Like other viewers, you generate a Content Provider to add content for Table, and one
Label Provider told Table Viewer to show content. Of course you have to set the Input Data.
The code should probably be like this:
TableViewer TableViewer = New TableViewer (PARENT);
TableViewer.SetContentProvider (New MyContentProvider ());
TableViewer.SetLabelProvider (New MylabelProvider ());
TableViewer.setInput (MyData);
***** Content Provider
Like ListViewer, Content Provider must implement IstructureContentProvider interface,
The IStructureContentProvider interface defines the following three methods:
Method descriptin
Void Dispose () call this party when TableViewer is called by Dispose
Law. In this method, you should dispose all you have
Generate and requires an object of Dispose.
Object [] getElements (Object When TableViewer needs to list each line for the table
INPUTELEMENT calls this method. Return to a row of data for your table.
Void InputChanged (Viewer Viewer, the current internal data structure change
OldInput, Object NewInput calls this method, this method will provide data change
specific measure.
For example, suppose a ArrayList called MYLIST contains your data. Each object in MYLIS is a definition
A class called widget, which contains a name, a color and a price. Each Widget also contains
A graphic. Your Content Provider may look like this:
Public class mycontentProvider imports istructuredContentProvider {
Public void dispose () {
/ / Nothing need to need dispose
}
Public Object [] getElements (Object INPUTELEMENT) {// INPUTELEMENT, THE INPUT DATA, IS MYLIST
Return ((List) MyList) .toarray ();
}
Public void InputChanged (Viewer Viewer, Object OldInput, Object NewInput) {
// Nothing to do
}
}
The getElemets () method returns all Widgets in the form of an array. All objects in the array will become a line in the table.
However, if there is no Label Provider, there will be no things in the form, if you are visible, you must generate
A label provider.
***** label provider
Lable Provider must implement the ITABLABELPROVIDER interface, you need to implement the following 6 methods:
Method
Void AddListener adds a listener as this label provider
(When ILABLEPROVIDER LISTENER calls this method, add Listener to your own
Listener) a list.
Void Dispose () call this party when TableViewer is called by Dispose
Law. In this method, you should dispose all you have
Generate and requires an object of Dispose.
Image getColumnImage is in a specific column you want to increase a certain cell
(Object Element, int image. Returns NULL if you don't want to set pictures
ColumnIndex)
Boolean IslabelProperty When TableViewer wants to determine a specific cell-specific
(Object Element, when there is an impact on Label when the String property changes
Property) This method. If you change will affect the Lable of a specific cell
Return to True, otherwise returns false.
Void Removelistener When you move a listener from Label Provider
(IlabelProviderListener calls this method. In this method, you will be from your own
List drove in List.
Take the above Widget as an example, your Label Provider should probably look like this:
Public class mylabelprovider imports itablelabelprovider {
// Holds the listener
List listener = new arraylist ();
Public Void AddListener (IlabelProviderListener Listener) {
// add the listener
Listeners.Add (Listener);
}
Public void dispose () {
// No object Need Dispose - these widgets have pictures belonging to their own
}
Public Image getColumnImnImage (Object Element, Int ColumnIndex) {
// show the image by the name
IF (columnIndex == name_column)
Return (widget) .Getimage ();
Return NULL;
Public String getColumnText (Object Element, INT ColumnIndex) {
Widget W = (widget) Element;
Switch (columnIndex) {
Case name_column:
Return w.getname ();
Case Color_Column:
Return w.getcolor ();
Case Price_column:
Return w.Getprice ();
}
// Should Never Get Here
""; "
}
Public Boolean IslabelProperty (Object Element, String Property) {
Return False;
}
Public Void Removelistener (IlabelProviderListener Listener) {
Listeners.Remove (Listener);
}
}
You must define column constants (name_column, color_column, and price_column) in a place. They
Corresponding to columns in the table. Remember that you must generate a table column for your form, the code looks like this:
// Get the underlying TABLE
Table Table = TableViewer.getTable ();
New TableColumn (Table, SWT.LEFT) .Settext ("Name");
New TableColumn (Table, SWT.LEFT) .SETTEXT ("color");
New TableColumn (Table, SWT.LEFT) .SETTEXT ("Price");