Use of eclipse treeviewer (routine)

Source: Internet
Author: User

The content provider provides the content of the Tree node. in this example, the data of the node is generated in the following code. In actual application, the actual data generated in another class is obtained through the getelements (Object inputelement method.

Getelements (Object inputelement, getchildren (Object parentelement), haschildren (object element) are required.

Package com. gujin;

Import org. Eclipse. jface. Viewers. ilabelprovider;
Import org. Eclipse. jface. Viewers. ilabelproviderlistener;
Import org. Eclipse. jface. Viewers. itreecontentprovider;
Import org. Eclipse. jface. Viewers. treeviewer;
Import org. Eclipse. jface. Viewers. Viewer;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Graphics. image;
Import org. Eclipse. SWT. layout. filllayout;
Import org. Eclipse. SWT. Widgets. composite;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. shell;

/**
* Content providers. It determines which objects should be displayed in treeviewer.
*/
Public class mycontentprovider implements itreecontentprovider {
/**
* This method determines which objects are displayed at the top level of the tree. In this method, three country objects are generated.
* @ Param inputelement: the object input using the TV. setinput () method. This object is not used here.
*/
Public object [] getelements (Object inputelement ){
String [] OBJ = new string [3];
OBJ [0] = "China ";
OBJ [1] = "us ";
OBJ [2] = "UK ";
Return OBJ;
}

/**
* This method determines which child nodes should be displayed on the node. No matter what the parent node is, each node has three word nodes.
* @ Param parentelement the clicked node (parent node)
*/
Public object [] getchildren (Object parentelement ){
String [] OBJ = new string [4];
OBJ [0] = "Guilin ";
OBJ [1] = "Beijing ";
OBJ [2] = "New York ";
OBJ [3] = "London ";
Return OBJ;
}

/**
* Determine whether a node has a subnode. In this case, 3721 is returned.
* True: All nodes have subnodes. At this time, there is a "+" icon in front of the node
* @ Param element: Determine whether a subnode exists.
*/

Public Boolean haschildren (object element ){

Return true;
}

/**
* Obtain the parent node of a node. This method is rarely required
*/
Public object getparent (object element ){
Return NULL;
}

/**
* This method is executed when the treeviewer is destroyed. This method is rarely required
*/
Public void dispose (){}

/**
* This method is called when TV. setinput () changes. This method is rarely required
*/
Public void inputchanged (viewer, object oldinput, object newinput ){}
}

The following classes provide labels, such as the node name and dot icon.

Package com. gujin;
Import org. Eclipse. jface. Viewers. ilabelprovider;
Import org. Eclipse. jface. Viewers. ilabelproviderlistener;
Import org. Eclipse. jface. Viewers. itreecontentprovider;
Import org. Eclipse. jface. Viewers. treeviewer;
Import org. Eclipse. jface. Viewers. Viewer;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Graphics. image;
Import org. Eclipse. SWT. layout. filllayout;
Import org. Eclipse. SWT. Widgets. composite;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. shell;

/**
* The tag provider. It is mainly responsible for displaying (text and image) each single object in treeviewer)
*/
Public class mylableprovider implements ilabelprovider {

/**
* What images are displayed?
* @ Param Node
* @ Return can be a null value.
*/
Public Image getimage (object element ){
// Isharedimages nvidimages = platformui. getworkbench (). get1_dimages ();
// Return nvidimages. getimage (isharedimages. img_objs_warn_tsk );
Return NULL;
}

/**
* What text is displayed?
* @ Param Node
* @ Return cannot be a null value.
*/
Public String gettext (object element ){
Return (string) element;

}

Public void addlistener (ilabelproviderlistener listener ){}

Public void dispose (){}

Public Boolean islabelproperty (object element, string property ){
Return false;
}

Public void removelistener (ilabelproviderlistener listener ){}
}

/**
* The tag provider. If you make the above mylableprovider useless methods,
* In eclipse, there is a default implementation labelprovider of ilabelprovider, which can be inherited.
*/
// Private Static final class mylableprovider extends labelprovider {
// Public String gettext (object element ){
// Return (string) element;
//}
//}

 

 

Finally, the treeviewer is displayed.

Package com. gujin;

Import org. Eclipse. jface. Viewers. treeviewer;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. layout. filllayout;
Import org. Eclipse. SWT. Widgets. composite;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. shell;

Public class creattree {
Public static void open (){
Final display = New Display ();
Final shell = new shell ();
Shell. setlayout (New filllayout ());
UI (Shell );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ())
Display. Sleep ();
}
}

Private Static void UI (shell ){
Composite c = new composite (shell, SWT. None );
C. setlayout (New filllayout ());

Treeviewer TV = new treeviewer (C, SWT. Border );
TV. setcontentprovider (New mycontentprovider ());
TV. setlabelprovider (New mylableprovider ());
TV. setinput (new object (); // The method used to set the input object is the same as that used in the previous step.

}
}

 

Related Article

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.