File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes paintComponent in JDesktopPane gives unexpected results when dragging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "paintComponent in JDesktopPane gives unexpected results when dragging" Watch "paintComponent in JDesktopPane gives unexpected results when dragging" New topic
Author

paintComponent in JDesktopPane gives unexpected results when dragging

Steven Rombauts
Greenhorn

Joined: Apr 21, 2009
Posts: 5
Hello Ranchers,

I need to draw a line between two JInternalFrames in a JDesktopPane.
I have created a subclass of JDesktopPane and added my line drawing into the paintComponent() method. The line is drawn, but when I am dragging the frames around, I get some very weird behaviour. Please look at the following two little screenshots:
This is with the desktoppane's dragMode set to LIVE_DRAG_MODE :

and this is with the dragMode OUTLINE_DRAG_MODE:


As soon as I release the frame I am dragging around, and resize the window, the line gets drawn correctly. I tried adding a swing Timer to call repaint() on the desktoppane more frequently, but this still gave me some weird results during dragging (although the correct line will now be drawn as soon as I release one of the frames)

Here is a quick sample code which should compile and give you an idea of my problem :



I put a gradient background color in there just to see how that responds to the dragging but this seems to be drawn very nicely during the frame dragging.

I am hoping someone might have some tips as to why I am having this problem.
It's not such a big deal but it looks so very ugly!

Thank you in advance for any help,
Steven
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Your problem is that the JDesktopPane is not painting itself after the internal frames are moved. You can solve this by adding a ComponentListener to the JInternalFrames and calling repaint() on the JDesktopPane in the componentMoved method, like so:



You will need to decide what to do when/if your internal frames are iconified or removed, and may also need to use an InteralFrameListener as well.
Steven Rombauts
Greenhorn

Joined: Apr 21, 2009
Posts: 5
Thank you very much pete stein, that did the trick!!

Thank you for your time :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: paintComponent in JDesktopPane gives unexpected results when dragging
 
Similar Threads
Help with Scrollable Image please...
Linking internal frames with a line
JInternalFrame : interaction
JInternalFrames + JSliders
Internal Frames Problems...need help