SWT listener

原创 2007年10月10日 15:25:00
 

这次做的项目中用到了SWT包设计界面,在做的过程中有一些widget的常用的事件处理,整理如下:
为一个widget添加Listener,调用 addListener() .其函数原形:
void addListener(int eventType, Listener listener)
eventType为SWT类定义的事件常量 . 每个常量代表程序中的一个事件,比如:SWT.FocusIn,SWT.MouseDoubleClick,SWT.KeyDown等. 常用的事件类型参照表A。第二个参数Listener 对象可以是一个已经定义好有名字的Listener对象,也可以是匿名类,可以是内部类(inner class)也可以是外部类(outer class),不过大多使用匿名的内部类, 例如为一个按钮添加点击事件的监听器,代码如:

button.addListener(SWT.Selection, new Listener() { 
public void handleEvent(Event e) { 
  switch (e.type) { 
     case SWT.Selection: 
     System.out.println("Button pressed"); 
      break; 
  } 
 } 
}); 

Table A: Event Types

Type

Description

SWT.Activate

Triggered when the widget becomes the active window

SWT.Arm

Triggered when the widget is armed

SWT.Close

Triggered when the widget is closed

SWT.Collapse

Triggered when a tree node is collapsed

SWT.Deactivate

Triggered when the widget is no longer the active window

SWT.DefaultSelection

Triggered when the default selection occurs

SWT.Deiconify

Triggered when the widget is restored from being minimized

SWT.Dispose

Triggered when the widget is disposed

SWT.DragDetect

Triggered when the widget is dragged

SWT.Expand

Triggered when a tree node is expanded

SWT.FocusIn

Triggered when the widget gains focus

SWT.FocusOut

Triggered when the widget loses focus

SWT.HardKeyDown

Triggered when a special hardware key, such as on a Pocket PC device, is pressed

SWT.HardKeyUp

Triggered when a special hardware key, such as on a Pocket PC device, is released

SWT.Help

Triggered when the user requests help

SWT.Hide

Triggered when the widget is hidden

SWT.Iconify

Triggered when the widget is minimized

SWT.KeyDown

Triggered when the user presses a key

SWT.KeyUp

Triggered when the user releases a key

SWT.MenuDetect

Triggered when a menu is selected

SWT.Modify

Triggered when the text of a widget is modified

SWT.MouseDoubleClick

Triggered when the mouse is double-clicked

SWT.MouseDown

Triggered when the mouse button is clicked

SWT.MouseEnter

Triggered when the mouse pointer enters the widget

SWT.MouseExit

Triggered when the mouse pointer exits the widget

SWT.MouseHover

Triggered when the mouse pointer hovers over the widget

SWT.MouseMove

Triggered when the mouse pointer moves through the widget

SWT.MouseUp

Triggered when the mouse button is released

SWT.Move

Triggered when the widget is moved

SWT.None

Null event

SWT.Paint

Triggered when the widget is painted

SWT.Resize

Triggered when the widget is resized

SWT.Selection

Triggered when the widget is selected

SWT.Show

Triggered when the widget is shown

SWT.Traverse

Triggered when the user tabs through the controls

SWT.Verify

Triggered when the text for the widget is about to change, allowing you to veto the change

 SWT提供了很多事件监听器的接口(Interface)和适配器(Adapter),可以为控件添加相应的监听器。

为一个文本框添加按键监听器代码形如:

txtSample.addKeyListener(new KeyAdapter() { 
 public void keyPressed(KeyEvent event) { 
  switch (event.keyCode) { 
     case SWT.KEYPAD_CR: 
     case SWT.CR:
       System.out.println("Enter Key be pressed!");
           break; 
  } 
 } 
}); 

为一个按键添加单击事件监听器,代码形如:

btnSample.addSelectionListener(new SelectionAdapter() { 
 public void widgetSelected(SelectionEvent event) { 
  //在这里添加按钮单击事件的处理代码
 } 
}); 

给文本框添加文本框内容改变的监听器,代码形如:

txtSample.addModifyListener(new ModifyListener() { 
 public void modifyText(ModifyEvent event) { 
  //在这里添加文本框文本内容改变的处理代码
  //一般地,要先移除该文本框的所有监听器以避免不必要的死循环
  //然后在最后把原来的监听器添加回来
 } 
}); 

控件的鼠标双击事件也是经常用到的,其添加监听器的代码形如:

shell.addMouseListener(new MouseListener() { 
 public void mouseDoubleClick(MouseEvent event) { 
  //在这里添加控件双击事件的处理代码  
 } 
}); 

另外,当程序中要处理数据输入时,SWT包提供的输入验证监听器非常有用,它可以避免用户的无效数据输入,其添加监听器的代码形如:

/*Called when user types into text box,but before the text box gets what the user typed*/
txtSample.addVerifyListener(new VerifyListener() { 
 public void verifyText(VerifyEvent event) { 

  //下面是用户输入验证的程序代码示例
  //Assume you don't allow it
  event.doit = false;
  //Get the Character typed
  char myChar = event.character;
  //Allow 0~9
  if ( Character.isDigit(myChar) ) event.doit = true;
  //Allow Backspace
  if ( myChar == '/b' ) event.doit = true;
  else{
    MessageBox msgBox = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
    msgBox.setText( "输入错误!" );
    msgBox.setMessage( "你不能输入除数字和退格键以外的字符!" );
    msgBox.open();
  } 
 } 
}); 

最后再给一个如何处理消息框用户按了哪个键的代码例子:

MessageBox msgBox = new MessageBox( shell, SWT.YES | SWT.NO | SWT.ICON_QUESTION );
msgBox.setText( "消息框的标题" );
msgBox.setMessage( "消息框的提示文字" );
switch( msgBox.open() ){
//msgBox.open()返回所选按钮的常量
  case SWT.YES:
      //程序处理
      break;
  case SWT.NO:
      //程序处理
      break;
}
 

实现自定义的SWT事件和Listener

继承java.util.EventObject(xxx), 提供一些get方法让客户端获取事件发生的信息。 继承java.util.EventListener(xxxListener),...
  • atusoft
  • atusoft
  • 2007-12-04 16:46:00
  • 694

Swt中实现对TitleAreaDialog窗口的关闭进行监听

某天在进行TitleAreaDialog窗口关闭控制的时候
  • u012006909
  • u012006909
  • 2014-08-10 14:13:21
  • 450

SWT:Tabfolder TabItem Table TableItem etc 的使用。

1.Eclipse中的相对路径为:"."+File.separator+"bin"+File.separator+"file.type"; 2.Tabfolder TabItem   比较可笑的是在编...
  • cyklucifer
  • cyklucifer
  • 2010-04-13 18:19:00
  • 5086

SWT/Jface 事件监听

1. 获取焦点事件: [java] view plain copy print? text.addListener(SWT.FocusIn, new List...
  • sidihuo
  • sidihuo
  • 2016-06-17 17:42:54
  • 1525

SWT TEXT基本事件实例

import org.eclipse.swt.SWT;import org.eclipse.swt.events.ModifyEvent;import org.eclipse.swt.events.M...
  • naughty610
  • naughty610
  • 2010-02-16 12:12:00
  • 7329

SWT Listener说明

这次做的项目中用到了SWT包设计界面,在做的过程中有一些widget的常用的事件处理,整理如下: 为一个widget添加Listener,调用 addListener() .其函数原形: voi...
  • unetman
  • unetman
  • 2013-03-18 16:59:16
  • 659

swt 关闭对话框 关闭dialog

在使用swt 的dialog的时候(不是jface的dialog),用了eclipse的向导生成的,然后对话框有一个父窗口 构造函数如下 public MyDialog(Shell paren...
  • linuxchyu
  • linuxchyu
  • 2012-10-23 20:31:17
  • 2032

使用exe4j把jar转换成exe文件时,报错java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Composite

在使用Eclipse 导出jar后,使用exe4j做成exe文件时保错,java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Composit...
  • jia611
  • jia611
  • 2014-12-21 15:55:52
  • 3484

Eclipse下配置swt开发环境

现在真的是落伍了,直到现在我才知道什么是swt以及swt是干什么的。这两天有时间搞了一下,感觉还是很不错的,这篇文章主要是记录如何安装和配置swt。都是别人写过多少遍的文章了,虽然内容类似,但是我还是...
  • wyp_810618
  • wyp_810618
  • 2008-04-04 17:59:00
  • 11295

SWT开发Java应用程序GUI入门

第一次做了Java GUI,我选择用elipse自己的前段开发工具 SWT/JFace。这篇文章是基于eclipse MARS.2.  用SWT编写的GUI的风格如下:                ...
  • sahusoft
  • sahusoft
  • 2016-07-10 11:29:41
  • 15054
收藏助手
不良信息举报
您举报文章:SWT listener
举报原因:
原因补充:

(最多只允许输入30个字)