Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to resize JPanel size to JFrame size  RSS feed

 
Hrishikesh Hemade
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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)
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • 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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!