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 Prevent resizing of JPanel in BorderLayout 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 "Prevent resizing of JPanel in BorderLayout" Watch "Prevent resizing of JPanel in BorderLayout" New topic
Author

Prevent resizing of JPanel in BorderLayout

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Hi, I am trying to add a JPanel to the center region of a BorderLayout. The JPanel should be 200 by 200, but when I add it directly to the BorderLayout, it is stretched out. I need it to be just 200 by 200 pixels. I have tried adding another JPanel with a FlowLayout, and adding my JPanel to that, then adding that to the BorderLayout. But when I do that, my JPanel shrinks down to maybe 10 by 10 pixels. This is frustrating, I hate swing. It seems like it never works quite the way it's supposed to... Anyway, is there something I'm missing? Here's my code:


Studying for SCJP 6
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

  • What is DisplayArea?
  • You need to call displayArea.setPreferredSize(new Dimension(200,200));. I am presuming DisplayArea is some kind of JPanel


  • [How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
    Michael Dunn
    Ranch Hand

    Joined: Jun 09, 2003
    Posts: 4632
    see if this gets you any closer
    (had to create a DisplayArea - use it first to see the border outline, then remove the class, recompile with your own DisplayArea)

    Eric Daly
    Ranch Hand

    Joined: Jul 11, 2006
    Posts: 143
    Thanks, that works. I changed the layout of centerPanel back to flowlayout though, because I don't understand gridbaglayout (yet). And yes, by the way I just have a separate class that starts up DrawString and the DisplayArea is just a JPanel. So really all I needed to do was change my JPanel code from setSize(200, 200) to setPreferredSize(new Dimension(200, 200)). Why should that make a difference? What is the difference between the two methods, and why doesn't it work when I use setSize? It seems like that's the method I should use... pretty annoying if you ask me.
    Michael Dunn
    Ranch Hand

    Joined: Jun 09, 2003
    Posts: 4632
    layoutManager manage the size, trying to honor a component's preferredSize
    in no layout manager (null) setSize will work
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Prevent resizing of JPanel in BorderLayout
     
    Similar Threads
    Attaching components to JPanel
    Determining width and height of a String
    Using JTextFields in JTabbedPane
    Resizing Panels inside a Jframe ??
    Need SERIOUS HELP!!