SWT中树的监听器举例

来源:互联网 发布:newman 复杂网络 编辑:程序博客网 时间:2024/05/26 14:10
import org.eclipse.swt.SWT;import org.eclipse.swt.custom.SashForm;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Tree;import org.eclipse.swt.widgets.TreeItem;public class SashFormDemo {public static void main(String[] args) {Display display = new Display();final Shell shell = new Shell(display);shell.setLayout(new FillLayout());SashForm sashForm = new SashForm(shell,SWT.BORDER|SWT.HORIZONTAL);sashForm.setLayout(new FillLayout());Composite left = new Composite(sashForm,SWT.NONE);left.setLayout(new FillLayout());final Tree tree = new Tree(left,SWT.V_SCROLL|SWT.H_SCROLL);TreeItem root1 = new TreeItem(tree,SWT.NONE);root1.setText("Root");root1.setData("root");TreeItem root11 = new TreeItem(root1,SWT.NONE);root11.setText("Root11");root11.setData("11");TreeItem root12 = new TreeItem(root1,SWT.NONE);root12.setText("Root12");root12.setData("12");TreeItem root2 = new TreeItem(tree,SWT.NONE);root2.setText("Root2");root2.setData("2");Composite right = new Composite(sashForm,SWT.NONE);right.setLayout(new FillLayout());final Label labR = new Label(right,SWT.NONE);labR.setText("Right");root1.setExpanded(true); tree.addListener(SWT.MouseUp, new Listener(){@Overridepublic void handleEvent(Event event) {Point point = new Point(event.x,event.y);TreeItem item = tree.getItem(point);if(item!=null){labR.setText((String)(item.getData()));}}}); shell.open();while(!shell.isDisposed()){if(!display.readAndDispatch()){display.sleep();}}display.dispose();}}


原创粉丝点击