• Post Reply Bookmark Topic Watch Topic
  • New Topic

ItemListener/Textlistener  RSS feed

 
Helmut Lerch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to add a ItemListener and TextListener to all Components within a Container (awt). I know how much and which Components in the Container not until runtime. I use the following Code, but it does'nt look very OOP. Does anybody know if there is another way?
<code><pre>
TextItemListener implements TextListener, ItemListener {
...
}
class Test {
private TextItemListener textItemListener = new TextItemListener();
.....
void addListenerToComponents(Component comp) {
Component[] childs = comp.getComponent();
for ( int i = childs.length; i > 0; i-- ) {
if ( childs[i] instanceof Container ) {
addListenerToComponents(childs[i]);
}
else {
if (childs[i] instanceof TextComponent) {
childs[i].addTextListener(textItemListener);
}
elseif ( childs[i] instanceof Choice) {
childs[i].addItemListener(textItemListener);
}
elseif ( childs[i] instanceof Checkbox ) {
childs[i].addItemListener(textItemListener);
}
elseif ( childs[i] instanceof List ) {
childs[i].addItemListener(textItemListener);
}
}
}
}
}
</pre></code>
Thanks
[This message has been edited by Helmut Lerch (edited May 21, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!