• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

paintComponent in JDesktopPane gives unexpected results when dragging

 
Steven Rombauts
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much pete stein, that did the trick!!

Thank you for your time :-)
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic