16296

Java element icons SWT ECLIPSE

Question:

How to add the java element icons (e.g. class, methods, package, etc as that in the package explorer) in SWT's Tree control?

e.g, I have the following tree structure: somePackage

--> somePackage | |--> someClass | |--> someMethod

Here --> represents the folding icon. I want to keep this intact but additionally I want to have the appropriate icon for respective java element (as it is visible in the package explorer). I am using SWT Tree to built the tree view. following is the code to construct it:

final Tree tree = new Tree (composite, SWT.BORDER); for (int i=0; i<4; i++) { // Package TreeItem iItem = new TreeItem (tree, 0); iItem.setText ("TreeItem (0) -" + i); for (int j=0; j<4; j++) { // Class TreeItem jItem = new TreeItem (iItem, 0); jItem.setText ("TreeItem (1) -" + j); for (int k=0; k<4; k++) { // Method or Fields TreeItem kItem = new TreeItem (jItem, 0); kItem.setText ("TreeItem (2) -" + k); } } } tree.setBounds(25, 50, 580, 200);

Answer1:

You can add the dependency org.eclipse.jdt.ui to your project to get access to the shared images of the JDT project. The following code works for Eclipse 3.x. For plain SWT applications (no Eclipse plugin) you need to add a bunch of eclipse plugins (including org.eclipse.jdt.ui) to get this functionality in your project.

import org.eclipse.jdt.ui.ISharedImages; import org.eclipse.jdt.ui.JavaUI; ... ISharedImages images = (ISharedImages) JavaUI.getSharedImages(); Image image = images.getImage(ISharedImages.IMG_OBJS_CLASS); // class file icon

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/Jrv93.png" data-original="https://i.stack.imgur.com/Jrv93.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

<strong>EDIT:</strong><br /> If you don't want to include all the libraries only because you want to use a a few eclipse images: <a href="http://shinych.blogspot.de/2007/05/eclipse-shared-images.html" rel="nofollow">I found a list of all eclipse shared images in the web</a>. (based on eclipse 3.4)

<strong>Source:</strong><br /><a href="http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/ui/ISharedImages.html" rel="nofollow">interface org.eclipse.jdt.ui.ISharedImages</a>

Answer2:

Doesn't <a href="http://help.eclipse.org/helios/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/widgets/TreeItem.html#setImage%28org.eclipse.swt.graphics.Image%29" rel="nofollow">TreeItem.setImage(Image)</a> do what you want?

public static void main(String[] a) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("StackOverflow"); shell.setLayout(new FillLayout()); Tree tree = new Tree(shell, SWT.SINGLE | SWT.BORDER); TreeItem child1 = new TreeItem(tree, SWT.NONE); child1.setText("1"); child1.setImage(display.getSystemImage(SWT.ICON_INFORMATION)); TreeItem child11 = new TreeItem(child1, SWT.NONE); child11.setText("1_1"); TreeItem child2 = new TreeItem(tree, SWT.NONE); child2.setText("2"); child2.setImage(display.getSystemImage(SWT.ICON_ERROR)); TreeItem child22 = new TreeItem(child2, SWT.NONE); child22.setText("2_2"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } <hr />

If you are just looking for the Eclipse icons, you can find them <a href="http://eclipse-icons.i24.cc/" rel="nofollow">here</a>.

Recommend

  • Interface implements a different interface, causing issues binding to Combobox
  • Invalid variance: The type parameter 'T' must be invariantly valid on 'xxx.IItem.GetL
  • Implement OnClickListener on FastAdapter implementing IItem
  • ListView_GetItem() macro & LV_GETITEMTEXT returns empty STRING while getting another APP listvie
  • JavaFx: In TreeView Need Only Scroll to Index number when treeitem is out of View
  • How to attach data to TreeItem in SWT/Java?
  • Textview values does not update when data received from Arduino
  • Why is this code not working? Hangman
  • Extend Name Box
  • addressing in assembler
  • f:param to composite components
  • data.table replicate rows after join?
  • JavaScript overloading with a callback
  • UIScrollView setContentOffset: animated: not working
  • What is the quartz default thread count
  • how to increment a message header
  • Entity Framework ObjectContext: Concurrency
  • drawing random circles, storing their coorindates in an array
  • Hector: how to query parts of a Composite Type
  • Regarding starting the threads on a condition
  • Linq Merge lists
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • how to adjust image in a panel in Java swing?
  • How can I send an e-mail from a vbs script
  • Change JButton Shape while respecting Look And Feel
  • QLineEdit password safety
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Trying to switch camera back to front but getting exception
  • jquery mobile loadPage not working
  • Free memory of cv::Mat loaded using FileStorage API
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • Java static initializers and reflection
  • unknown Exception android
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app