SWT中樹的監聽器舉例


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(){

			@Override
			public 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();
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章