Not sure if what I need is possible. I have a container (JPanel) that contains some internal elements. I was wondering if it is possible to force internal elements to fit into the container's size. I need them to be fully visible i.e., resize to fit inside the Panel's size and not cut some parts of the internal elements.
Scrolling is not an option.
Is this possible by using a Layout or something?
EDIT: Important clarification: The thing is that I do not have access to the internal elements neither to their properties so I would say that a Layoutmanager capable of resizing child elements to fit to its size is needed. I tested BorderLayout and GridBagLayout but the result is always the same, the internal elements are cut out.
It's for exactly that reason that LayoutManagers exist. All the
LayoutManagers
work for simple containers directly, excludingGridBagLayout
which is to able to handle most complete GUIs directly.For most compete GUI you have some choices as follows:
JPanel
and each has childJPanels
with same or differentLayoutManager
GridBagLayout
You could set the
JPanel
layout to border layout, then add the single child to the center. If there are multiple children, this approach becomes less useful since components added to the theNORTH
,SOUTH
,EAST
, andWEST
will remain statically sized while the centre resizes to fill the remainder.In short, this isn't an ideal solution. All layouting in Swing is made all the more complex by the fact that different components behave in different ways, so you really need to provide further details of the child components you wish to add to your panel, and any behaviour that has been overridden on those components.
The best way is to try a couple of simple examples to see what mileage you get and whether subtle redesign of your child component nesting could help.
I was using a JInternalFrame inside JDesktopPane. I wanted the internal_frame to auto resize as desktop pane is resized, so I had to implement the AncestorResized event for the internal frame where I placed the following code:
and it worked for me :)
you can use a layout, like GridBagLayout, or BorderLayout depending on the situation. With proper weights it is possible.
this sounds to me like you should just peek an appropriate layout manager and use it. For example, look at BorderLayout - put your component in the CENTER and it will occupy all the area. Its up to each concrete layout manager to decide what will be the size of the components. Mark