com.ccg.swing
Class SizeFixer

java.lang.Object
  extended by java.awt.event.ComponentAdapter
      extended by com.ccg.swing.SizeFixer
All Implemented Interfaces:
ComponentListener, EventListener

public class SizeFixer
extends ComponentAdapter

Keeps a graphical component from becoming too small or too large.

It is a pain in the but to:

This class takes care of this problem by adding a component listener on the object which monitors the resize events. If any resize event attempts to set the size of the component to a value which is too small or to large - we will automatically fix the width and/or height of the component to fit in the allowed limits.

The following code fragment demonstrates how you can keep a JComponent such that its size is somewhere between the original component size and 95% of the available screen dimensions:



public static void noMoreThan95(JComponent c) {
  SizeFixer sf = new SizeFixer(c);
  Dimension maxSize = sf.getMaximumSize();
  maxSize.width = (maxSize.width*90/100);
  maxSize.height = (maxSize.height*90/100);
}
 
 

Since:
1.0
Version:
$Revision: 1.3 $
Author:
$Author: pkb $

Constructor Summary
SizeFixer(Component c)
          Construct the object and install it as a component listener for the component supplied.
 
Method Summary
 void componentResized(ComponentEvent ce)
          Check the new size of a component.
 Dimension getMaximumSize()
          Get the maximum size this component is allowed to become.
 Dimension getMinimumSize()
          Get the minimum size this window is allowed to become.
 void setMaximumSize(Dimension val)
          Set the maximum size this component is allowed to become.
 void setMinimumSize(Dimension val)
          Set the minimum size this window is allowed to become.
 String toString()
          Returns a simple string representation of limits enforced.
 
Methods inherited from class java.awt.event.ComponentAdapter
componentHidden, componentMoved, componentShown
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SizeFixer

public SizeFixer(Component c)
Construct the object and install it as a component listener for the component supplied.

This creates the object and registers it as a component listener on the component you specify (which must not be null). It also defaults the minimum size of the component to be the current size (so you may want to either "pack" your component first, or then call setMinimimumSize() to change the desired minimum size afterwards). The maximum size will default to 100% of the screen width/height.

Parameters:
c - The Component you want to preserve the minimum size of - must not be null.
Since:
1.0
See Also:
setMinimumSize(java.awt.Dimension)
Method Detail

setMaximumSize

public void setMaximumSize(Dimension val)
Set the maximum size this component is allowed to become.

When initially constructed, the maximum size is limited to the size of the screen. You can remove a maximum limit by passing null to this method.

Parameters:
val - New Dimension value to assign - must not be null.
See Also:
getMaximumSize()

getMaximumSize

public Dimension getMaximumSize()
Get the maximum size this component is allowed to become.

Returns:
Direct reference to the Dimension object that manages the maximum values (never returns null).
See Also:
setMaximumSize(java.awt.Dimension)

setMinimumSize

public void setMinimumSize(Dimension val)
Set the minimum size this window is allowed to become.

You typically only need to use this method if your associated component was not at its minimum allowed size at the time of construction (or if you want to change the minimum allowed size).

Parameters:
val - New Dimension value to assign - must not be null.
See Also:
getMinimumSize()

getMinimumSize

public Dimension getMinimumSize()
Get the minimum size this window is allowed to become.

Returns:
Direct reference to the Dimension object that manages the minimum values (never returns null).
See Also:
setMinimumSize(java.awt.Dimension)

componentResized

public void componentResized(ComponentEvent ce)
Check the new size of a component.

This method is automatically invoked when the user resizes the associated component. If we don't like the size specified, we will force it back to where we think it should be (preserve the minimum size).

Specified by:
componentResized in interface ComponentListener
Overrides:
componentResized in class ComponentAdapter
Parameters:
ce - The ComponentEvent which triggered the action. Must not be null and its source must be a non-null Component object type.
Since:
1.0

toString

public String toString()
Returns a simple string representation of limits enforced.

Overrides:
toString in class Object
Returns:
Simple string representation of object.
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.