Skip to content

Instantly share code, notes, and snippets.

@JaDogg
Last active August 3, 2021 20:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaDogg/4d9999ba233f4a43b7c5 to your computer and use it in GitHub Desktop.
Save JaDogg/4d9999ba233f4a43b7c5 to your computer and use it in GitHub Desktop.
JavaFX Custom Decorator
package openpimtests;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
/**
* JavaFX Custom Decorator
*
* @author Bhathiya
*/
public class CustomDecorator extends AnchorPane {
private double xOffset = 0;
private double yOffset = 0;
private Stage primaryStage;
public CustomDecorator(Stage stage, Node node) {
super();
primaryStage = stage;
this.setPadding(new Insets(0, 0, 0, 0));
// load css :
//this.getStylesheets().add("/openpimtests/Catra.css");
Button btnMax = buildButton("Mx",(e) -> {
primaryStage.setMaximized(!primaryStage.isMaximized());
});
AnchorPane.setRightAnchor(btnMax, 60.0);
AnchorPane.setTopAnchor(btnMax, 10.0);
Button btnClose = buildButton("X",(e) -> {
primaryStage.close();
});
AnchorPane.setRightAnchor(btnClose, 10.0);
AnchorPane.setTopAnchor(btnClose, 10.0);
AnchorPane.setTopAnchor(node, 0.0);
AnchorPane.setRightAnchor(node, 0.0);
AnchorPane.setBottomAnchor(node, 0.0);
AnchorPane.setLeftAnchor(node, 0.0);
this.getChildren().addAll(node,btnMax,btnClose);
this.setOnMousePressed((MouseEvent event) -> {
xOffset = event.getSceneX();
yOffset = event.getSceneY();
});
this.setOnMouseDragged((MouseEvent event) -> {
primaryStage.setX(event.getScreenX() - xOffset);
primaryStage.setY(event.getScreenY() - yOffset);
});
}
private Button buildButton(String text, EventHandler<ActionEvent> onAction) {
Button btn = new Button(text);
btn.setMinSize(44, 44);
btn.setMaxSize(44, 44);
//set a style
//btn.getStyleClass().add("white-soft-button");
btn.setOnAction(onAction);
return btn;
}
}
package openpimtests;
import java.awt.Paint;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* @author Bhathiya
*/
public class OpenPIMTests extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); // load an fxml
stage.initStyle(StageStyle.TRANSPARENT); //undecorated/transparent
Scene scene = new Scene(new CustomDecorator(stage, root)); // create a scene from new CustomDecorator
scene.setFill(null);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
@JaDogg
Copy link
Author

JaDogg commented Jul 12, 2014

OpenPIMTests.java --> How to use the new custom decorator

@kunalpalekar222
Copy link

I don't even want that min and close button. What should I do to remove that min and close button?
Please Reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment