Results 1 to 3 of 3
- 07-19-2010, 03:43 AM #1Member
- Join Date
- Jul 2010
- Rep Power
Problems regarding JPanels in JPanels
We are working on a medium sized game in Java using Swing, and Model View Controller architecture.
Our JFrame Class is called GameRunner, which adds GameView to it's Content Pane, and then it validates and repaints.
GameView extends JPanel with a Y_AXIS BoxLayout and adds a WorldPanel and a HudPanel.
HudPanel extends Jpanel with a X_AXIS BoxLayout and adds a DescriptionPanel and a ActionPanel
DescriptionPanel extends JPanel with a X_AXIS BoxLayout and adds a JButton for testing.
ActionPanel extends JPanel with a X_AXIS BoxLayout and adds a JTextArea for testing.
WorldPanel extends JPanel, and overrides JPanel's paint method and uses it to paint a 2d array of images (Which represents our world in our game).
Inside the paint override, we set the size of the WorldPanel every single time it paints. Without that call, the WorldPanel defaults to a (0,0) Dimension for some ungodly reason.
So, our setSize in the paint fixed this problem (Or so we thought). We had it running perfectly inside of GameView before we added the HudPanel. However, when we did add the HudPanel to the GameView funny things occurred.
The HudPanel places itself at the very top of the GameView, as if there were no WorldPanel at all. The HudPanel stretches to fill all of GameView.
It might be worth mentioning that we do set the size of the HudPanel when we create it.
I think that at this point you'll have to post some code. I recommend that you create and post the smallest program possible that is compilable, runnable, demonstrates your problem, and has no extraneous code not related to the problem, an SSCCE. Please see the link for details on how to create one of these because trust me, if your SSCCE is compliant with the specs, you'll likely get a very helpful answer from someone here quickly. Since your question is about layouts, your SSCCE should only contain identifiable JPanels added using the same layouts that you use in your program.
Much luck and welcome to the forum!
- 07-19-2010, 08:33 PM #3
1. For Swing components, override paintComponent, not paint.
2. Never change the state of a component in a painting method override.
3. Don't use setSize, use an appropriate layout manager.
4. If you still need to set a size (the container doesn't contain any components so defaults to [0, 0] or maybe [10, 10] then use setPreferredSize which is respected by most layout managers (there are some that respect the minimum/maximum size)
Also, I second the recommendation to post a SSCCE.
- By owain1221 in forum Java AppletsReplies: 7Last Post: 11-15-2009, 11:21 PM
- By maverik_vz in forum AWT / SwingReplies: 1Last Post: 03-12-2009, 11:46 AM
- By dousedfirepants in forum New To JavaReplies: 3Last Post: 11-13-2008, 05:47 AM
- By Mastergeek666 in forum AWT / SwingReplies: 1Last Post: 01-19-2008, 12:32 AM
- By Soda in forum New To JavaReplies: 3Last Post: 12-08-2007, 05:54 PM