I'm making a computer version of one of my favourite board games, called Lord of the Rings: The Confrontation. I am using Swing for the GUI.
In the game, you move around game pieces on a board. Each piece is named after a character or beast in the books, and has its own unique properties. I want to be able to bring up a small box that displays these properties when I right-click it (or rather click the piece using any other button than MouseEvent.BUTTON1).
So I have a subclassed JPanel, ActorInfoPanel, that I've drawn up using NetBeans's GUI tool (which is kind of overkill, but I need to learn how to use it anyway). I catch the "right click" on a piece and create a transparent glass pane, with an instance of ActorInfoPanel displayed in the middle of the board. Behind it, I have a semi-transparent black JPanel acting as a drop shadow.
The problem is that on certain occasions, the ActorInfoPanel gets the wrong location, and I have no idea why.
Here's how it looks just before I click a piece representing the Witch King:
Here's how it looks after I've clicked it for the first time (note that the drop shadow is exactly where it should be, and its location is set using the values for the ActorInfoPanel's location... huh.):
The interesting thing is that the debugger tells me that it's supposed to have its proper position (x=187, y=179 or whatever), but still it appears at x=0, y=0!
Now, on subsequent clicks, it looks the way it should:
The panel now also gets the correct position if I click any other piece, say Saruman or Gollum. However, if I try to click the "Mouth [of Sauron]" piece, the problem appears again:
The pattern I see is that the caption "Mouth of Sauron" is so wide that the panel becomes wider than its preferred width (which in itself is perfectly fine). Still doesn't explain to me why the panel gets glued onto the top left corner, though. If I alternate clicking between Mouth and any other, the problem is persistent.
It would seem that whenever the ActorInfoPanel has a "new" width, it gets put in the top left corner. If it has the same width as the previously displayed one, everything works as it should.
If anyone could aid me in figuring out why this beaviour occurs, I'd be very grateful!
Below is the code for the glass pane and info panel classes.
I'm curious as to why you add the JPanel "p" to both a JFrame, hackFrame, and to a JPanel, pane? I wonder if this is messing you up. Have you tried just getting rid of hackFrame as it doesn't seem to be doing anything other than be a potential source of problems?