I have a JPanel using the BorderLayout containing a JPanel in the NORTH region and a JPanel in the SOUTH region. This JPanel in turn is contained in the SOUTH region of my JFrame. Sometimes when I start the program I only see one of the two panels, but when the mouse is moved a few times the other panel pops into view and the rest of the program layout shifts in accordance and is seen as I have intended. I have tried a number of things such as using setPreferredSize() but the same thing keeps happening, mind you it does not happen at every startup. Any hints as to how I might resolved this annoyance is well appreciated. Thanks.
Joined: Jan 14, 2004
upperBottomPanel = new ImagePanel(img); upperBottomPanel.setLayout(null); When you add components to a container the parent components (container) layout manager asks each of the children for their size requirements and makes arrangements to show each component according to its needs. Some layout managers practically ignore the size requirements of the child components, eg, GridLayout. Some layout managers pay special attention to the preferred size of the child components and attempt to show them at this size. Other layout managers fall in between these extremes. All is documented in the class api for the layout managers and in the tutorial pages on layout managers. When you specify null for a layout manager then you must supply the layout information to the parent container so it knows what to do with the children you have added to it. We do this with setBounds and setLocation. After realization setSize also works. The preferredSize is useful for layout managers that honor it in Swing and in j2se 1.5+ in the AWT. Your choices are to either use one or a combiation of layout managers, or to specify bounds for each of the components that you want shown in containers with null layout managers. To learn about layout managers see Lesson: Laying Out Components Within a Container.