This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Here's a challenging little problem I need to solve. Below is a test project whose sole purpose is to demonstrate, what I call, a "paintDragPreview". What I mean by this is show the drawn outline of a component as it is being dragged to another position on a JPanel. For purposes of simplicity and education, I'm not actually going to relocate the component in this test project. The component can stay where it is. I'm just going to use the normal FlowLayoutManager to place the component on a JPanel. In a larger project I am actually using an XYLayoutManager which allows me to place a component anywhere on the JPanel after I finish dragging it to another location.
For this test project, in order to draw an outline of a component, I needed layers of JPanels. One that has its layout manager set to OverlayLayout(...), another named WhiteBoard (where I place the component), and another on top of the WhiteBoard named glassPane. It is on the glassPane that I draw an outline of the component as it is being dragged across the WhiteBoard. So far, so good.
Here's the problem I face at this juncture and you can copy/paste, compile and run the code below to confirm the behaviour of this test project so far. As you place your mouse cursor over the LabelComponent the cursor changes to the crosshairs cursor indicating that the component can be moved. As you press the left mouse button the current coordinates of the LabelComponent is registered. As you begin to drag the component the glassPane draws a rectangle representing the component and repaints for every pixel that you drag the mouse. What is supposed to happen next is on the mouseReleased(..) the component becomes relocated in the new position. However, as I said before, I'm not taking this test project that far.
I just want to show that as the rectangle is drawn on the glassPane we loose sight of the LabelComponent. It disappears! We can't see anything below the glassPane! This is because we set the glassPane.setVisible(...) method to true while dragging and back to false upon mouseReleased(...). This is necessary in order to see what's being drawn on the glassPane. However, this is not the behaviour I want.
DESIRED BEHAVIOUR: I want to both see the component below the glassPane and also see the rectangle as it is being repainted as we drag the mouse across the WhiteBoard. I've played with the setOpaque(...) method for the glassPane but that doesn't seem to help much.
As a reference, I consulted the example for Glass Pane at the following URL: http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html When you run that example project you can still see the components below the Glass Pane the whole time while the red dot is being painted on the Glass Pane. I fail to see a difference between that example project and mine. Perhaps someone can help on this one?
[ UD: added line breaks to keep a nice layout ] [ October 08, 2007: Message edited by: Ulf Dittmer ]