2
votes

Can I add a listener (let's say MouseAdapter) to a Swing component and all it's internal decoration components?

So that when a JInternalFrame is moved by the mouse (by dragging its window title bar), it would give me following events:

  • mousePressed event,
  • mouseDragged event,
  • mouseReleased event.

Currently, I receive none of the above events when dragging JInternalFrame.

I hope there is some standardized solution, but I couldn't find any.

EDIT:
Some people suggest using ComponentListener, but that wouldn't do for me. I need to know, when the user stops dragging (mouseReleasedEvent), not when the component moves.

5

5 Answers

2
votes

Yes, you can add a listener to all a container's components. getComponents and add the listener. You should be able to manage to do this recursively. You can also use ContainerListener to check for adding and removing components.

However, MouseListener and MouseMotionListener behave strangely in that the event normally bubbles up to the parent, but does not do so if a listener is present (how is that for hopeless design?).

Your choices are:

  • Recursively adding listeners (bad, see above)
  • Adding listeners to specific components (fragile)
  • Adding a "glass pane" (a messy hack)
  • Adding an AWTEventListener to Toolkit (requires permissions)
  • Pushing an EventQueue and checking through events (doesn't work of Opera and Safari apparently; stops system copy-and-paste and applet dragging from working)
  • Use ComponentListener?
1
votes

I found out how it could be done, but something tells me, it's a dirty hack ;)

Well, it works, but who can give me the guarantee that it works everywhere?

// ctor goes here {

InternalFrameUI thisUI = getUI();
((BasicInternalFrameUI) thisUI).getNorthPane()
    .addMouseMotionListener(new MyMouseListener());

// }

NorthPane turns out to be the window title bar.

0
votes

You should probably use a MouseMotionListener instead of a MouseListener.

0
votes

In the JInternalFrame API documentation, it says:

Generally, you add JInternalFrames to a JDesktopPane. The UI delegates the look-and-feel-specific actions to the DesktopManager object maintained by the JDesktopPane.

Maybe you should add your listener to the JDesktopPane.

0
votes

MouseListener/MouseMotionListener wont detect when dragging a JInternalFrame. Your best bet here to detect movement is using a ComponentListener on the JInternalFrame itself.