14

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.

cksrc
  • 2,062
  • 3
  • 24
  • 39
  • 2
    `LayoutManager` is the obvious solution. If you say how exactly you want to display your components, may be someone will suggest you a preferable `LayoutManager`. – Mohayemin Sep 14 '11 at 08:05
  • 1
    The 'obvious' answer is to expand the components (e.g. a `JTree`), add them to layouts with no `JScrollPane`, call `pack()` on the frame and then `frame.setMinimumSize(frame.getSize());`. Job done. – Andrew Thompson Sep 14 '11 at 10:11
  • It sounds strange that you can't access properties of "internal elements", thos elements are `JComponent`s right? Then, you can access to any of the standard properties of `JComponent`, sizes in particular. – jfpoilpret Sep 14 '11 at 11:51

5 Answers5

14

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 complete GUI's you have some choices as follows:

  • Look for a 3rd party 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 the same or different LayoutManager
  • Or custom layout, should be hard..., but same as using GridBagLayout
Jasperan
  • 2,154
  • 1
  • 16
  • 40
mKorbel
  • 109,525
  • 20
  • 134
  • 319
6

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
extorn
  • 611
  • 1
  • 5
  • 11
0

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();
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
iltaf khalid
  • 9,928
  • 5
  • 30
  • 34
  • as a general recommendation: [don't use setXXSize, ever](http://stackoverflow.com/a/7229519/203657) - arguably internal frames can be viewed as a bit of an exception, as their sizing with respect to the parent desktopPane isn't controlled by LayoutManagers. But then, the question isn't about internalframes, is it :-) – kleopatra Apr 25 '14 at 10:12
0

you can use a layout, like GridBagLayout, or BorderLayout depending on the situation. With proper weights it is possible.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
0

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

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97