重要的是列表项的选中事件。我们需要提供接口让程序员能够添加事件监控器(listener)来监控发生的事件,并且一旦发生事件,我们需要通知监控器。
首先,我们添加一个成员来保存添加的事件监控器:
Vector selectionListeners = new Vector();
我们再增加一个函数addSelectionListener,让程序员可以添加监控器
public void addSelectionListener(SelectionListener listener) {
selectionListeners.addElement(listener);
}
在我们前面的代码中,我们注意到每次选择项改变,我们都会调用selectionChanged函数。下面是selectionChanged函数代码。这里,我们会生成一个 SelectionEvent事件,并且逐个调用事件监控器的widgetSelected方法。这样别人就可以监听到我们的事件了。
public void selectionChanged() {
Event event = new Event();
event.widget = this;
SelectionEvent e = new SelectionEvent(event);
for (int i = 0; i < selectionListeners.size(); i++) {
SelectionListener listener = (SelectionListener) selectionListeners.elementAt(i);
listener.widgetSelected(e);
}
}
?a·¢Eclipse×??¨ò????t(5)
ê±??:2011-02-09 IBM áo?1
???ú?¨?ú1|?ü£¨Accessibility£?ò2è?ò?3é?aèí?t??òaμ?2?·?£??üê?μ?2D?2è?ò2?ü1?·?±?μ?ê1ó??ò??μ?èí?t?£?à1úò??-ᢷ¨£?2?·?o?Accessibility1?·?μ?èí?t2??ü1??ú?t??2????úê??£?ò???a·¢μ????tò2Dèòa?§3?Accessibility.????μ?′ú??ê1?ò??μ????tóDAccessibility?§3??£???D×???òaμ?ê?getRoleoígetValueoˉêy?£?ò??μ????tê?′óCanvas?ì3D£??ò???úgetRoleoˉêy?D·μ??ACC.ROLE_LIST,?a?ù?ò??μ????t2??üè??á?????áèí?t???ò??μ????t×÷?aáD±í???t??′y?£
Accessible??accessible??=??getAccessible();
?????? accessible.addAccessibleControlListener(new??AccessibleControlAdapter()??{
?????????? public??void??getRole(AccessibleControlEvent??e)??{
?????????? int??role??=??0;
?????????????? int??childID??=??e.childID;
?????????????? if??(childID??==??ACC.CHILDID_SELF)??{
?????????????????? role??=??ACC.ROLE_LIST;
?????????????? }??else??if??(childID??>=??0??&&??childID??<??colors.size())??{
?????????????????? role??=??ACC.ROLE_LISTITEM;
?????????????? }
?????????????? e.detail??=??role;
?????????? }
?????????? public??void??getValue(AccessibleControlEvent??e){
?????????? int??childID??=??e.childID;
?????????????? if??(childID??==??ACC.CHILDID_SELF)??{
?????????????????? e.result??=??getText();
?????????????? }??else??if??(childID??>=??0??&&??childID??<??colors.size())??{
?????????????????? e.result??=??(String)colorNames.get(childID);
?????????????? }
?????????? }
?????????? public??void??getChildAtPoint(AccessibleControlEvent??e)??{
?????????? Point??testPoint??=??toControl(new??Point(e.x,??e.y));
?????????????? int??childID??=??ACC.CHILDID_NONE;
?????????????? childID??=??(testPoint.y??-??cy)/lineHeight;
?????????????? if??(childID??==??ACC.CHILDID_NONE)??{
?????????????????? Rectangle??location??=??getBounds();
?????????????????? location.height??=??location.height??-??getClientArea().height;
?????????????????? if??(location.cont
|