swing/jdialog dragable in specific area only.

hello,

i have a swing interface, where i want a JDialog to be draggable only in a specific area. (pretty much exactly the area of the main window)

i tried it with a componentListener and setting the dialog back, whenever it is out of the specified bounds. unfortunately this leads to heavy flickering due to the jumping back of the JDialog.

(JInternalFrames are no option.)

thanks for any help!

Why are JInternalFrames out of question?

technical issues with lightweight-containers and jogl’s GLCanvas that are not fixed until java7. :slight_smile:

You can use heavyweight component (like Panel) inserted directly to JFrame (it will be on top of everything lightweight and also heavyweight if z order is set properly).

Does overriding setLocation/setBounds help?

Maybe the JVM catches the events from the OS, and passes them to the Component via those methods.

Probably not though…

Yeah your only hope is overriding something so that you can catch the desired positioning before the default stuff actually moves it. My best guess would be SetLocation like Riven said, although there might be something you can do in a window listener somewhere.

It’s my understanding that it happens the other way around, when a heavy-weight Container is moved it is all handled within the native peer.
setLocation/setBounds are there to allow the Java app. to request the Containers native peer do its ‘magic’ to move the Component.

:edit: removed erroneous comment

ComponentPeer is pretty tucked away. windowlistener doesn’t help.

If you can find a way it will be pretty hackery.

Just implement your own JInternalframe with whatever you are allowed to use. It’s really not that complicated

I’m adding more concrete howto:

  1. create Panel and store content of your dialog there (both AWT and Swing allowed here)
  2. call setBounds for your Panel and add it to yourJFrame.getRootPane(), when added into root pane it’s not affected by JFrame’s layout manager, so you can set positon and size by setBounds
  3. create your own JComponent for dialog frame (to which you add real dialog content), which you can use to move around and/or resize the dialog (the JInternalFrame can be probably tweaked to this, but I’m not sure how much work it is)

Don’t forget to use reliable source for mouse location, eg. using location relative to parent is a bad choice, even if converted to absolute coordinates as the native events order is undefined and unsynchronized. Use MouseInfo class instead.