File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prevent resizing of JPanel in BorderLayout

 
Eric Daly
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • 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:
 
Maneesh Godbole
Saloon Keeper
Posts: 10968
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
  •  
    Michael Dunn
    Ranch Hand
    Posts: 4632
    • 0
    • Mark post as helpful
    • send pies
    • 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
    • 0
    • Mark post as helpful
    • send pies
    • 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
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    layoutManager manage the size, trying to honor a component's preferredSize
    in no layout manager (null) setSize will work
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic