Minimum Size Frame


Implements a subclass of ULCFrame that cannot be made smaller than a certain minimum size. Is useful when you have content that needs a minimum size to be layouted correctly.

How to use

Create the frame and set its minimum size.

ULCMinimalSizeFrame frame = new ULCMinimalSizeFrame("MinimumSizeFrame");
frame.setMinimumSize(new Dimension(400, 300));

How it is implemented

The solution chosen adds a component listener to the client-side frame that ensures the minimum size of the frame every time the frame is resized.

Add the Component Listener client-side

We add the component listener to the basic frame in the restoreState() method. The restoreState() method is invoked by the ULC framework for each created client-side proxy. The getBasicComponent() returns the Swing component the proxy stands for, i.e. a JFrame in our case.

public void restoreState(Anything args) {

getBasicComponent().addComponentListener(new MinimumSizeHandler()); }

Enforce the minimum Size

The component listener enforces the minimum size whenever the basic frame is resized. The frame is enlarged if either the width or the height of the JFrame is smaller than the minimum size.

private void enforceMinimumSize() {
    if (fMinimumSize != null) {
        if (fMinimumSize.width > getBasicComponent().getWidth()
            || fMinimumSize.height > getBasicComponent().getHeight()) {

int width = Math.max(fMinimumSize.width, getBasicComponent().getWidth()); int height = Math.max(fMinimumSize.height, getBasicComponent().getHeight());

getBasicComponent().setSize(width, height); } } }

private class MinimumSizeHandler extends ComponentAdapter { public void componentResized(ComponentEvent event) { enforceMinimumSize(); } }


Source code -