Forums Register Login

Prevent resizing of JPanel in BorderLayout

+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
  • What is DisplayArea?
  • You need to call displayArea.setPreferredSize(new Dimension(200,200));. I am presuming DisplayArea is some kind of JPanel
  • +Pie Number of slices to send: Send
    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)

    +Pie Number of slices to send: Send
    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.
    +Pie Number of slices to send: Send
    layoutManager manage the size, trying to honor a component's preferredSize
    in no layout manager (null) setSize will work
    Crusading Chameleon likes the size of this ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 8998 times.
    Similar Threads
    Attaching components to JPanel
    Resizing Panels inside a Jframe ??
    Determining width and height of a String
    Using JTextFields in JTabbedPane
    Need SERIOUS HELP!!
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 18, 2024 22:27:10.