Last active
April 2, 2021 09:24
-
-
Save Da9el00/777bcd6b138314edd2330fd910e6bdf8 to your computer and use it in GitHub Desktop.
How to access Stage from a Controllers Initialize - https://www.youtube.com/watch?v=BHLDfI30WWw&lc=Ugxam2ZTD-Vouf68Pf94AaABAg
This file contains 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
package sample; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Button; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.scene.shape.Polygon; | |
import javafx.stage.Stage; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
private double x = 0,y = 0; | |
private Stage stage; | |
public void setStage(Stage stage){ | |
this.stage = stage; | |
} | |
@FXML | |
private Polygon polygon; | |
@FXML | |
void close(MouseEvent event) { | |
Stage stage = (Stage) ((Button)event.getSource()).getScene().getWindow(); | |
stage.close(); | |
} | |
@FXML | |
private AnchorPane topBar; | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
topBar.setOnMousePressed(mouseEvent -> { | |
x = mouseEvent.getSceneX(); | |
y = mouseEvent.getSceneY(); | |
}); | |
topBar.setOnMouseDragged(mouseEvent -> { | |
stage.setX(mouseEvent.getScreenX() - x); | |
stage.setY(mouseEvent.getScreenY() - y); | |
}); | |
} | |
} |
This file contains 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
/* | |
#303139 - Background | |
#323339 - TopBar | |
#F75590 - Close | |
*/ | |
.background{ | |
-fx-background-color: #303139; | |
-fx-border-color: #303139; | |
} | |
.topBar{ | |
-fx-background-color: #323339; | |
} | |
.button{ | |
-fx-background-color: #303139; | |
} | |
.button:hover{ | |
-fx-background-color:#F75590; | |
} |
This file contains 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
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
import javafx.stage.StageStyle; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml")); | |
Parent root = loader.load(); | |
Controller controller = loader.getController(); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.initStyle(StageStyle.UNDECORATED); | |
primaryStage.setScene(new Scene(root, 600, 400)); | |
controller.setStage(primaryStage); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.geometry.Insets?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.layout.HBox?> | |
<?import javafx.scene.text.Font?> | |
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" styleClass="background" stylesheets="@CSS.css" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<AnchorPane fx:id="topBar" prefHeight="35.0" prefWidth="600.0"> | |
<children> | |
<HBox alignment="CENTER_RIGHT" prefHeight="35.0" prefWidth="600.0" styleClass="topBar" stylesheets="@CSS.css"> | |
<children> | |
<Button mnemonicParsing="false" onMouseClicked="#close" stylesheets="@CSS.css" text="X" textFill="WHITE"> | |
<font> | |
<Font size="13.0" /> | |
</font> | |
</Button> | |
</children> | |
<padding> | |
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> | |
</padding> | |
</HBox> | |
</children> | |
</AnchorPane> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment