java swt 设置图标,Java元素图标SWT ECLIPSE

本文介绍了如何在SWT的Tree控件中添加Java元素图标,如类、方法和包等,以实现类似Eclipse Package Explorer的显示效果。关键在于引入Eclipse JDT库获取共享图片,并通过ISharedImages接口设置图标。
摘要由CSDN通过智能技术生成

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);

解决方案

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

Jrv93.png

EDIT:

If you don't want to include all the libraries only because you want to use a a few eclipse images: I found a list of all eclipse shared images in the web. (based on eclipse 3.4)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值