tooltip + F2

         在ECLIPSE中开发JAVA程序,当我们打开一个JAVA类时,ECLIPSE会打开一个JDT的JAVA EDITOR来显示这个类.当把鼠标移到某一个JAVA类型时,会出现一个tooltip的提示信息.这时我们可以按F2来把焦点设定在这个提示信息窗口上,这样就不会因为一移动鼠标这个提示窗口就消失了.        
         tooltip大家都会加,但怎么把F2这个功能也加上去呢?
         其实很容易,下面我用简单的代码介绍一下:
         比如说我们要在Table上加上tooltip,当鼠标移到某一行时,显示该行的提示信息.
         我们可以这么做:
         TableViewer viewer
         Table table = viewer.getTable();
         TooltipListener tooltipListener = new TooltipListener(viewer);
         table.addListener(SWT.KeyDown,tooltipListener);
         table.addListener(SWT.MouseHover,tooltipListener);
         这里的TooltipListener类就是一个监听器,来监听系统的两个事件:一个是键盘敲下,一个是鼠标停留.
            private class TooltipListener implements Listener {
                   private Shell tipshell;
                   private Text msg;
                   private TableViewer viewer;
                   private Shell shell;
                   private Display display;

                   public TooltipListener(TableViewer viewer) {
                         this.viewer = viewer;
                         shell = this.viewer .getControl().getShell();
                         display = shell.getDisplay();
                    }

                    public void handleEvent(Event event) {
                          if(event.type == SWT.KeyDown){
                               if(event.keyCode == SWT.F2){
                                    tipshell.forceFocus();
                                    tooltiptext.setFacus();
                               }
                          }else if(event.type == SWT.MouseHover){
                               TableItem item = viewer.getTable().getItem(new Point(event.x, event.y));
                               tipShell = new Shell(shell, SWT.ON_TOP | SWT.TOOL);
                               tipShell.setLayout(new FillLayout());
                               msg = new Text(tipShell, SWT.WRAP);
                               msg.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
                               msg.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
                               msg.setText("hello!nihao");
                               tipShell.setVisible(true);
                          }
                }
                break;
            }
       }
       当鼠标停留,这个监听器监听到了停留事件就打开一个tooltip:hello!nihao 这时如果按下F2,这个监听器就监听到,马上把鼠标焦点定位到tooltip上.
       ^_^,就这么简单!其实就是两个语句:tipshell.forceFocus();tooltiptext.setFacus();
       希望对大家有帮助.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值