• 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

how to resize JPanel size to JFrame size

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have frame size set to (750,415) and used pack()so that all the componenets are displayed and i have added an WindowStateChaged Listener to see frame has been resize. After the window state change then i tooked the size of the frame ie Dimension size=frame.getSize()/super.getSize(); then i set that size to panel size. panel.setSize(size); then revaildate()/repaint(); i have used but the size of panel is not getting set to Frame size so what is the problem and how to solve it.

Thank you.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the panel is a JPanel, and it is not a null layout, you should be using
panel.setPreferredSize(new Dimension( , ));

if your panel is the only component added to the frame, it should resize
along with the frame (actually, it can't do anything else), but it sounds like
you must have other components added to the frame. If so, if you can add the
panel to the frame at BorderLayout.CENTER, it should increase/decrease along
with the frame (without any additional code)
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) If you use setSize() then pack() you just undid your setSize() to begin with. pack() will attempt to size the window to be as big as necessary to display all components at their preferred sizes.

2) Setting the size of the panel to the size of the frame is essentially impossible with the methodology you're using. The frame's size includes window decorations, the panel can't possibly be as big as the frame unless you've gone through the trouble of removing all window decorations and border(s) (extremely unlikely).

3) I don't understand this piece of code:



Where is that being called? By what?

4) If you want a single JPanel to "fill" a JFrame all you need to do is make that JPanel the contentPane of the JFrame using frame.setContentPane(). If you want to make a JPanel that "fills" another JPanel you need to add it appropriately using a LayoutManager such as BorderLayout and then adding the JPanel using BorderLayout.CENTER (as Michael referred to). However, since we're talking about (presumably) a JFrame, why don't you just make it the contentPane?
 
reply
    Bookmark Topic Watch Topic
  • New Topic