Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

swing

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.

like image 238
Shakur Avatar asked Sep 14 '11 07:09

Shakur


People also ask

How do you make a JFrame fit the screen?

Select all components of JFrame, right click, select 'Auto Resizing', check for both Horizontal and Vertical, close . Show activity on this post. Calling pack() will usually result in the window being resized to fit the contents' preferred size.

Which method is used for resizing the frame in Java?

To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.

What are the components and containers in swing?

Java Swing Bootcamp | Build Java GUI Applications With Swing Containers are an integral part of SWING GUI components. A container provides a space where a component can be located. A Container in AWT is a component itself and it provides the capability to add a component to itself.


1 Answers

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
like image 196
mKorbel Avatar answered Sep 28 '22 12:09

mKorbel