This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes [Swing] Strange issue with manual placement of a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "[Swing] Strange issue with manual placement of a panel" Watch "[Swing] Strange issue with manual placement of a panel" New topic
Author

[Swing] Strange issue with manual placement of a panel

Per Pettersson
Greenhorn

Joined: May 28, 2009
Posts: 4
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.




pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Swing] Strange issue with manual placement of a panel
 
Similar Threads
Revalidate problems
Saving drawing on simple paint application problem
palindrome not very proficient...
transparent panel in applet
GridBagLayout issues