Kapcash Kapcash - 1 year ago 108
Java Question

How to dynamically control auto-resize components in Java Swing

Creating a new GUI in Java (1.8) Swing, I am searching for a way to override resize behavior of all my components.

Let me explain to you with some edited photos:

1. Full Screen GUI

This is my full screen GUI, with 3 panels and a
. The green one needs to have a fixed size, the others would be resizable.

Currently, I only have 2 small
to organize them (one vertical, and one horizontal for green and cyan panels).

FullScreen GUI

2. Small horizontal resize

If I, for example, reduce the frame size from the right side, I want my blue and cyan panel to be resized according to the new frame size. But the green panel must be fixed. (Not the most difficult part I think.)

Resized once GUI

3. Minimum horizontal size

This is the most difficult part for me. As soon as the cyan (or blue) panel reach is minimum size, I want him to "push" the green panel to the left, even if it disappears off the frame.

Of course, pulling the frame to the right will make the green panel appear again.

Minimum size of the GUI

How could I do it?

I thought of using
or a specific listener to manually decide the resize behavior but I need some advice.

Sorry if an existing post can answer this, I didn't find any answer explaining the issue for me.

Thanks in advance!

If you want more examples, look at the "Evernote" software which acts the same way

Answer Source

Setting the max/min/preferred size of the Green panel can keep that panel the same size under the first condition. To check for resizes, you can use a ComponentListener on one of the other JPanel's - if the size gets below a particular width then change the max/min/preferred size of the Green panel.

Below is a hacked together example of doing this - it resizes the Green panel when the Blue is < 600, and the resize is a weighted resize (30% of total width). To get the true L&F and that you desire you may have to play with the layout/sizes.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

public class GridTest extends JPanel{

    private boolean changeAllowed = false;
    //keep reference to cyan for the height dimension
    final JPanel cyan = new JPanel();

    public GridTest(){
        cyan.setPreferredSize(new Dimension(600, 300));//provide sizing hint

    private Dimension getCustomDimensions(){
        if ( changeAllowed ){
            return new Dimension((int)(super.getParent().getBounds().width * 0.3), cyan.getBounds().height);
            return new Dimension(200, cyan.getBounds().height);
    public Dimension getMaximumSize(){
        return getCustomDimensions();
    public Dimension getMinimumSize(){
        return getCustomDimensions();
    public Dimension getPreferredSize(){
        return getCustomDimensions();

    public static void main(String[] args) throws Exception{
        SwingUtilities.invokeAndWait(new Runnable(){

            public void run() {
                final int MINIMUM = 600;
                JFrame frame = new JFrame();
                frame.add(new JToolBar(), BorderLayout.NORTH);
                final JPanel blue = new JPanel();

                final GridTest green = new GridTest();

                blue.setPreferredSize(new Dimension(900, 300));//hint at size
                blue.setMinimumSize(new Dimension(100, 200));//hint at size
                //Nest Box Layouts
                Box top = Box.createHorizontalBox();
                Box bottom = Box.createHorizontalBox();
                Box vert = Box.createVerticalBox();
                //listen for resizes
                blue.addComponentListener(new ComponentAdapter(){

                    public void componentResized(ComponentEvent e) {
                        if ( blue.getBounds().width < MINIMUM ){//set flag
                            green.changeAllowed = true;
                            green.changeAllowed = false;

                frame.setSize(800, 600);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download