Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2010
    Rep Power

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    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.


Similar Threads

  1. Posistioning Jpanels
    By owain1221 in forum Java Applets
    Replies: 7
    Last Post: 11-15-2009, 11:21 PM
  2. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 11:46 AM
  3. Problems displaying and array of JPanels
    By dousedfirepants in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 05:47 AM
  4. Problems with JPanels and displaying
    By Mastergeek666 in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2008, 12:32 AM
  5. How to add Images to JPanels?
    By Soda in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 05:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts