Posted by Rob Camick on September 13, 2009
A recent entry on Moving Windows discussed how you might add functionality to move a component or a non decorated window. Today we will look at adding resizing functionality to these same components.
The ComponentResizer class will be used to implement the resizing functionality. Its design is similiar to the ComponentMover class in that you need to specify the drag insets for the component. The insets are used to recognize when the mouse is moved over a resizable border. When this happens the cursor is changed to the appropriate cursor to indicate the resizing border. One difference is that an inset value of 0 for any border implies that the border cannot be resized. The default value is 5 for each border. The same drag insets should be used for the ComponentResizer and ComponentMover so the classes don’t get confused over whether a component resize or move is required.
The other features of the class are as follows:
- snap size (default 1) – specifies the resize increment. That is the distance the mouse must be dragged before the border changes size. The border will snap to this value once dragging has reached the halfway mark.
- minimum size – specify the minimum size of the component. The minimum size is constrained by the drag insets. (ie. the width cannot be less that the sum of the insets left and right values)
- maximum size – specify the maximum size of the component. The size of the component will also be constrained by the size of its parent. When resizing a window or frame the constraint is the desktop size. For other components the constraint is the parent container.
Usage of the ComponentResizer is straight forward. There are various constructors you can use. One example would be:
ComponentResizer cr = new ComponentResizer();
cr.setSnapSize(new Dimension(10, 10));
Hopefully this class provides the flexibility you might need when resizing components.