Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DragResizercan be used to add mouse listeners to a Region and make it resizable by the user by clicking and dragging the border in the same way as a window. Only height resizing is currently implemented.
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
/**
* {@link DragResizer} can be used to add mouse listeners to a {@link Region}
* and make it resizable by the user by clicking and dragging the border in the
* same way as a window.
* <p>
* Only height resizing is currently implemented. Usage: <pre>DragResizer.makeResizable(myAnchorPane);</pre>
*
* @author atill
*
*/
public class DragResizer {
/**
* The margin around the control that a user can click in to start resizing
* the region.
*/
private static final int RESIZE_MARGIN = 5;
private final Region region;
private double y;
private boolean initMinHeight;
private boolean dragging;
private DragResizer(Region aRegion) {
region = aRegion;
}
public static void makeResizable(Region region) {
final DragResizer resizer = new DragResizer(region);
region.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
resizer.mousePressed(event);
}});
region.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
resizer.mouseDragged(event);
}});
region.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
resizer.mouseOver(event);
}});
region.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
resizer.mouseReleased(event);
}});
}
protected void mouseReleased(MouseEvent event) {
dragging = false;
region.setCursor(Cursor.DEFAULT);
}
protected void mouseOver(MouseEvent event) {
if(isInDraggableZone(event) || dragging) {
region.setCursor(Cursor.S_RESIZE);
}
else {
region.setCursor(Cursor.DEFAULT);
}
}
protected boolean isInDraggableZone(MouseEvent event) {
return event.getY() > (region.getHeight() - RESIZE_MARGIN);
}
protected void mouseDragged(MouseEvent event) {
if(!dragging) {
return;
}
double mousey = event.getY();
double newHeight = region.getMinHeight() + (mousey - y);
region.setMinHeight(newHeight);
y = mousey;
}
protected void mousePressed(MouseEvent event) {
// ignore clicks outside of the draggable margin
if(!isInDraggableZone(event)) {
return;
}
dragging = true;
// make sure that the minimum height is set to the current height once,
// setting a min height that is smaller than the current height will
// have no effect
if (!initMinHeight) {
region.setMinHeight(region.getHeight());
initMinHeight = true;
}
y = event.getY();
}
}
@darmbrust

This comment has been minimized.

Copy link

commented Mar 4, 2014

Very very cool. I was about to try to write this... one search later, and here is perfectly working code.
Thanks!

@purringpigeon

This comment has been minimized.

Copy link

commented Jan 7, 2015

Hello, I was attempting to combine this with the following: https://gist.github.com/miho/3821180 however the two don't seem to work together. Do you know of a way to create a draggable and resizable pane? I would like to provide a user experience where the user may open several independent panes and move and size them as they desire. Ideally it would be Sizable, Draggable and Scrollable for when the size is made smaller than the original Pane. Thanks!

@mwang27

This comment has been minimized.

Copy link

commented Nov 28, 2015

Super nice DragResizer! I just make my stackpane FX node (, and all of its children FX nodes) perfectly resizable by add just one line: DragResizer.makeResizable(myStackpane);
Nice!

@cannibalsticky

This comment has been minimized.

Copy link

commented Apr 20, 2016

If anyone is interested, I made a little modification to the code in order to obtain both height and width resizing.

You can finde the code here: https://gist.github.com/cannibalsticky/a3057d6e9c1f029d99e6bc95f9b3340e

Happy coding!

@aabhaanjan

This comment has been minimized.

Copy link

commented Dec 23, 2017

is there any way to resize a decorated stage proportionally in square form ?

@fallingstar1204

This comment has been minimized.

Copy link

commented May 14, 2018

Cool! This is what I am looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.