• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Prevent resizing of JPanel in BorderLayout

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • What is DisplayArea?
  • You need to call displayArea.setPreferredSize(new Dimension(200,200));. I am presuming DisplayArea is some kind of JPanel
  •  
    Ranch Hand
    Posts: 4632
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 143
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 4632
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    layoutManager manage the size, trying to honor a component's preferredSize
    in no layout manager (null) setSize will work
     
    reply
      Bookmark Topic Watch Topic
    • New Topic