1.
获取焦点事件:
- text.addListener(SWT.FocusIn, new Listener() {
- public void handleEvent(Event e) {
- // 处理得到焦点的事件
- }
- });
失去焦点事件:
- text.addListener(SWT.FocusOut, new Listener() {
- public void handleEvent(Event e) {
- // 处理失去焦点的事件
- }
- });
鼠标移入移出事件:
- treeCom.getTree().addMouseTrackListener(new MouseTrackListener() {
- @Override
- public void mouseHover(MouseEvent e) { }
- @Override
- public void mouseExit(MouseEvent e) {
- //移入事件
- }
- @Override
- public void mouseEnter(MouseEvent e) {
- //移出事件
- }
- });
按钮单击事件:
- button.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- //事件处理
- }
- });
鼠标左键双击事件:
- tree.addListener(SWT.MouseDoubleClick, new Listener() {
- @Override
- public void handleEvent(Event event) {
- if(event.button != 1) { //按键不是左键跳出. 1左键,2中键,3右键
- return;
- }
- //事件处理
- }
- });
Table设置行高事件:
- table_1.addListener(SWT.MeasureItem, new Listener() {
- @Override
- public void handleEvent(Event event) {
- //处理事件
- event.height = 30;
- //把当前事件移除.不然你点一下表就会执行很多次事件.
- table.removeListener(SWT.MeasureItem, this);
- }
- });
按钮单击事件:
- button.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- //事件
- });
双击树.获得当前点击节点的名字:
- tree.addListener(SWT.MouseDoubleClick, new Listener() {
- @Override
- public void handleEvent(Event event) {
- if(event.button != 1) { //不是点击鼠标右键.
- return;
- }
- Point point = new Point(event.x, event.y); //获取当前点击的坐标点.
- TreeItem item = tree.getItem(point); //通过坐标点获取节点.
- //获取所有选中的树节点.
- //TreeItem [] itemArray = Global.getTree_UserOrgan().getTree().getSelection();
- System.out.println(tree.getText());
- }
- });
获取Eclipse界面的大小(size):
- Point size = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getSize();
获取鼠标当前位置:
- Point Point = Display.getCurrent().getCursorLocation();
获取屏幕分辨率:
- Monitor[] monitors = shell.getDisplay().getMonitors();
- for (int i = 0; i < monitors.length; i++) {
- Monitor m = monitors[i];
- System.out.println(m.getBounds().toString());
- }
- //程序输出: Rectangle {0, 0, 1366, 768}
树的复选框选中事件:
- tree.addListener(SWT.Selection, new Listener() {
- @Override
- public void handleEvent(Event event) {
- if (event.detail == SWT.CHECK) {
- TreeItem item = (TreeItem) event.item;
- if(item == null) {
- return;
- }
- boolean checked = item.getChecked();
- if(checked) { //如果是新选择一个元素. 就取消其他选择的元素.
- for (int i = 0; i < projectMoudelTree.getTreeItemList().size(); i++) {
- TreeItem tempItem = projectMoudelTree.getTreeItemList().get(i);
- if(item != tempItem) {
- tempItem.setChecked(false);
- }
- }
- }
- }
- }
- });
复选框选中事件:
- //创建复选框. 复选框不能带只读属性(SWT.READ_ONLY), 不然会认为你是按钮.
- checkbox = new Button(composite, SWT.CHECK);
- checkbox.setText("测试复选框");
- checkbox.addSelectionListener(new SelectionListener() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- //复选框, 选中取消时触发此事件.
- //checkbox.setSelection(false); 设置复选框选中.
- //如果想要复选框为只读属性
- if(checkbox.getSelection()) {
- checkbox.setSelection(false);
- } else {
- checkbox.setSelection(true);
- }
- }
- @Override
- public void widgetDefaultSelected(SelectionEvent e) { }
- });
SWT带垂直滚动条的Text自动滚动到底部(比如显示聊天信息, 日志的显示等)
以前一直以为是滚动条的设置. 结果完全不是那么回事.
- Text tbx_text = new Text(group, SWT.BORDER | SWT.WRAP
- | SWT.V_SCROLL | SWT.READ_ONLY);
- tbx_text.setTopIndex(Integer.MAX_VALUE);
Dialog模式化:
- private void createContents() {<span style="white-space:pre"> </span>
- shell = new Shell(getParent(), getStyle()|SWT.SHELL_TRIM|SWT.APPLICATION_MODAL);
2.