Last active
July 11, 2017 09:25
JavaFx internal window from my answer: http://stackoverflow.com/a/29956183/1996639
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.Cursor; | |
import javafx.scene.Node; | |
import javafx.scene.layout.Region; | |
/** | |
* @author zella. | |
*/ | |
public class InternalWindow extends Region { | |
//current state | |
private boolean RESIZE_BOTTOM; | |
private boolean RESIZE_RIGHT; | |
public void setRoot(Node node) { | |
getChildren().add(node); | |
} | |
public void makeFocusable() { | |
this.setOnMouseClicked(mouseEvent -> { | |
toFront(); | |
}); | |
} | |
//we can select nodes that react drag event | |
public void makeDragable(Node what) { | |
final Delta dragDelta = new Delta(); | |
what.setOnMousePressed(mouseEvent -> { | |
dragDelta.x = getLayoutX() - mouseEvent.getScreenX(); | |
dragDelta.y = getLayoutY() - mouseEvent.getScreenY(); | |
//also bring to front when moving | |
toFront(); | |
}); | |
what.setOnMouseDragged(mouseEvent -> { | |
setLayoutX(mouseEvent.getScreenX() + dragDelta.x); | |
setLayoutY(mouseEvent.getScreenY() + dragDelta.y); | |
}); | |
} | |
public void makeResizable(double mouseBorderWidth) { | |
this.setOnMouseMoved(mouseEvent -> { | |
//local window's coordiantes | |
double mouseX = mouseEvent.getX(); | |
double mouseY = mouseEvent.getY(); | |
//window size | |
double width = this.boundsInLocalProperty().get().getWidth(); | |
double height = this.boundsInLocalProperty().get().getHeight(); | |
//if we on the edge, change state and cursor | |
if (Math.abs(mouseX - width) < mouseBorderWidth | |
&& Math.abs(mouseY - height) < mouseBorderWidth) { | |
RESIZE_RIGHT = true; | |
RESIZE_BOTTOM = true; | |
this.setCursor(Cursor.NW_RESIZE); | |
} else { | |
RESIZE_BOTTOM = false; | |
RESIZE_RIGHT = false; | |
this.setCursor(Cursor.DEFAULT); | |
} | |
}); | |
this.setOnMouseDragged(mouseEvent -> { | |
//resize root | |
Region region = (Region) getChildren().get(0); | |
//resize logic depends on state | |
if (RESIZE_BOTTOM && RESIZE_RIGHT) { | |
region.setPrefSize(mouseEvent.getX(), mouseEvent.getY()); | |
} else if (RESIZE_RIGHT) { | |
region.setPrefWidth(mouseEvent.getX()); | |
} else if (RESIZE_BOTTOM) { | |
region.setPrefHeight(mouseEvent.getY()); | |
} | |
}); | |
} | |
public void setCloseButton(Button btn) { | |
btn.setOnAction(event -> ((Pane) getParent()).getChildren().remove(this)); | |
} | |
//just for encapsulation | |
private class Delta { | |
double x, y; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.Pane; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
Pane root = new Pane(); | |
root.getChildren().add(constructWindow()); | |
root.getChildren().add(constructWindow()); | |
primaryStage.setScene(new Scene(root, 300, 275)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
private InternalWindow constructWindow() { | |
// content | |
ImageView imageView = new ImageView("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Cheetah4.jpg/250px-Cheetah4.jpg"); | |
// title bar | |
BorderPane titleBar = new BorderPane(); | |
titleBar.setStyle("-fx-background-color: green; -fx-padding: 3"); | |
Label label = new Label("header"); | |
titleBar.setLeft(label); | |
Button closeButton = new Button("x"); | |
titleBar.setRight(closeButton); | |
// title bat + content | |
BorderPane windowPane = new BorderPane(); | |
windowPane.setStyle("-fx-border-width: 1; -fx-border-color: black"); | |
windowPane.setTop(titleBar); | |
windowPane.setCenter(imageView); | |
//apply layout to InternalWindow | |
InternalWindow interalWindow = new InternalWindow(); | |
interalWindow.setRoot(windowPane); | |
//drag only by title | |
interalWindow.makeDragable(titleBar); | |
interalWindow.makeDragable(label); | |
interalWindow.makeResizable(20); | |
interalWindow.makeFocusable(); | |
interalWindow.setCloseButton(closeButton); | |
return interalWindow; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment