Keeping ActionEvents seperate for each Overlay

What’s the best way to handle events for multiple overlays?

Is it best to have one mouse listener and one keyboard listener for your entire program then check which overlay has the focus to see where the actions should go.

Or should each overlay have it’s own listener and the listener only reacts if its respective overlay has focus.

I was thinking it would be better for each overlay to have it’s own listeners which start with an if statement checking for focus. If the overlay has focus then it does the appropriate action and consumes the event.

Has anyone had to deal with this before?

My overlays include things like chat windows so they do more then just display data. Thanks for the advice.

Also, I just wanted to thank everyone who has been so helpful lately. My classes are starting to wind down so I have a lot of time recently to program using xith3d for my own education and enjoyment. Hence, the large influx of questions to the boards.

Currently if you click on a UIWindow it will get keyboard focus and all keyboard events will be sent to it. Mouse events are sent to the UIWindow that the mouse is over. We have a new chat system which you can read about here (http://www.magicosm.net/magicosm_user_manual.pdf) and forgive me since the manual is not complete yet. We use the current Xith3D UI system to support our chat windows, so what you are asking to do is certainly possible with then current rev of Xith3D.

RIght now I have a UI which is a text area and when I type text into any keys which I catch for my main window still affect the main window.

In the main program I do …


                  Toolkit.getDefaultToolkit().addAWTEventListener(new KeyboardEventListener(keyEventList),AWTEvent.KEY_EVENT_MASK);

For the overlay I do


            UIEventAdapter eventAdapter = new UIEventAdapter(windowMgr);
            canvas.get3DPeer().getComponent().addKeyListener(eventAdapter);
            canvas.get3DPeer().getComponent().addMouseListener(eventAdapter);
            canvas.get3DPeer().getComponent().addMouseMotionListener(eventAdapter);
            canvas.get3DPeer().getComponent().setFocusable(true);

Then in the actual class which extend UIOverlay I go


            textFieldInput.addKeyListener(new KeyListener(){
                  public void keyPressed(KeyEvent e) {}

                  public void keyReleased(KeyEvent e) {}

                  public void keyTyped(KeyEvent e) {
                        //this notices when 'enter' is pressed and the string in the text field should be sent to the 
                        //text area for everyone to see when chatting
                        if(e.getKeyChar() == 10){
                              inputText = textFieldInput.getText();
                              System.out.println(inputText);
                              textFieldInput.setText(""); //clears the textfield after pressing enter
                              //textAreaScreen.append("\n" + inputText); //places the input text into the textArea
                              setMessage("\n" + inputText);
                              //client.processMessage(inputText);
                                    
                        }            
                              
                  }
                  
            });

I have a feeling I must be setting the listeners improperly. Are you able to tell just from those code snippets where I’m going wrong? Perhaps I’m setting one too many listeners or I’m setting up the main program listener improperly? Thank you very much for all your help.

--------------- EDIT ---------------

Okay, I looked through the UI source and I here’s what I think is going on. All events get caught by UIEventAdapter. UIEventAdapter then passes the event along to the overlay which has focus.

I got rid of the Toolkit.getDefaultToolkit code but now I’m not sure how I should set listeners for the main program window so that it receives events from the UIEventAdapter. Thanks again.
~shochu