|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.awt.event.ComponentAdapter
com.ccg.swing.SizeFixer
public class SizeFixer
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);
}
| 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 |
|---|
public SizeFixer(Component c)
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.
c - The Component you want to preserve the
minimum size of - must not be null.setMinimumSize(java.awt.Dimension)| Method Detail |
|---|
public void setMaximumSize(Dimension val)
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.
val - New Dimension value to assign - must not be null.getMaximumSize()public Dimension getMaximumSize()
setMaximumSize(java.awt.Dimension)public void setMinimumSize(Dimension val)
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).
val - New Dimension value to assign - must not be null.getMinimumSize()public Dimension getMinimumSize()
setMinimumSize(java.awt.Dimension)public void componentResized(ComponentEvent ce)
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).
componentResized in interface ComponentListenercomponentResized in class ComponentAdapterce - The ComponentEvent which triggered the
action. Must not be null and its source must be a non-null
Component object type.public String toString()
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||