简体   繁体   中英

Auto resize swing elements to fit to the container's size

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 ie, 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, excluding GridBagLayout which is to able to handle most complete GUIs directly.

For most compete GUI you have some choices as follows:

  • Look for a 3rd part layout such as MigLayout or here
  • Use GridBagLayout
  • Very easy way is use nested layout , where there is more than one JPanel and each has child JPanels with same or different LayoutManager
  • Or custom layout , should be hard..., but same as using 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 the NORTH , SOUTH , EAST , and WEST 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:

 this.setPreferredSize(this.getParent().getPreferredSize());
 this.pack();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM