[6022] in java-interest
Resizing a container from the size of its components
daemon@ATHENA.MIT.EDU (Riccardo Losco)
Fri Mar 15 22:31:56 1996
Date: Fri, 15 Mar 1996 17:14:28 --100
Reply-To: Java Interest <JAVA-INTEREST@JAVASOFT.COM>
From: Riccardo Losco <riccardo@IRSIPCS2-27-LE0.IRSIP.NA.CNR.IT>
To: Multiple recipients of list JAVA-INTEREST
<JAVA-INTEREST@JAVASOFT.COM>
Hi there, I've been working with Java for a couple of months so far,
and I've read most of the documentation available on line.
I haven't found any information to help me with this problem:
Given a Panel with an associated LayoutManager and some children
components (some are containers too), there isn't apparently any
trivial way of guessing the preferred size of the Panel given those of
its children.
I have some fixed size components (each has a different size however)
that I want to add and remove dinamically to a hierarchy of
containers-components.
My question is :
How can I know the preferredSize of a container given the ones of its
children ??
I add the first component, pack() and show() the main frame and
everything shows up correctly. Then, following user's instructions
(button click :-) ) I remove a component and add a new one with a
different size. I would, at this point, resize() the main frame so
that the new component shows up entirely.
I am using a GridBagLayout for the Panel that contains the object to
swap and a BorderLayout for the main frame.
At the moment my code is something like:
public class ResizeTest extends Frame {
// Here all the stuff to set up the panels, layouts. etc..
mainPanel = new Panel();
..
framePanel = new Panel();
framePanel.add(firstCanvas);
..
buttonPanel.add(button);
mainPanel.add("Center", framePanel);
mainPanel.add("South", buttonPanel);
add(mainPanel);
..
if (switched) {
switched = false;
framePanel.remove(secondCanvas);
framePanel.add("Center", firstCanvas);
framePanel.validate();
validate();
// here I would like to resize() the main frame
}
else {
switched = true;
System.out.println("was not Switched");
framePanel.remove(firstCanvas);
framePanel.add("Center", secondCanvas);
framePanel.validate();
validate();
// here I would like to resize() the main frame
}
-----
Thanks for any help
riccardo
=========================================
# ##### #### # ##### Riccardo Losco
# # # # # # #
# # # #### # # # IRSIP
# ##### # # ##### Via Pietro Castellino, 111
# # # # # # # 80131 Napoli
# # # #### # # Italy
http://www.irsip.na.cnr.it
Phone : +39 81 5904228 (Direct Line)
FAX : +39 81 5608330