Java FX Disable Stage Maximisation Animation - java

When I open a new window in my application and maximise it using newStage.setMaximized(true); it shows the animation of the window increasing in size from the default size to the size of the screen, is there any way to disable this animation and swiftly move from one fullscreen window to another upon button click?
Here's my code:
public class HomeController {
#FXML private StackPane ap;
public void ButtonClicked(ActionEvent e) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("PitchRecognition.fxml"));
Parent root = (Parent)loader.load();
Stage stage = (Stage) ap.getScene().getWindow();
stage.hide();
Stage newStage = new Stage();
Scene scene = new Scene(root);
newStage.setScene(scene);
newStage.setMaximized(true);
newStage.show();
}
}

Related

Creating new Transparent Stage on Event JavaFX

I'm trying to create a new Stage when a button is pressed.
It works but the problem is that I'd like this Stage to be fully transparent and lets us see what's behind the screen.
Code
Dimension Sizescreen = Toolkit.getDefaultToolkit().getScreenSize();
//Main stage with option menu
Pane window = new Pane();
Scene scene = new Scene(window);
stage.setTitle("Notification Extender");
//Create the button SetLooker
Button SetLooker = new Button("Set Looker");
//Add a Event when pressed
SetLooker.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//Create a sub-Stage
Pane subwindow = new Pane();
Scene subscene = new Scene(subwindow);
Stage substage = new Stage();
substage.setTitle("Notification Extender");
//Set this subStage Transparent
substage.initStyle(StageStyle.TRANSPARENT);
subscene.setFill(Color.TRANSPARENT);
substage.setWidth(Sizescreen.getWidth());
substage.setHeight(Sizescreen.getHeight());
substage.setX(0);
substage.setY(0);
//Create a a graphique element
Rectangle redrec = new Rectangle(120,40,50,50);
redrec.setStroke(Color.RED);
redrec.setStrokeWidth(2);
redrec.setFill(Color.TRANSPARENT);
//Add the graphique element to the sub-stage
subwindow.getChildren().add(redrec);
//Show the sub-stage
substage.setScene(subscene);
substage.show();
}
});
//Add the button to the main stage
window.getChildren().add(SetLooker);
//Show the main stage
stage.setScene(scene);
stage.show();
The problem is that when I press the button it shows the stage but it's not transparent at all it's completely white.
I've also tried to change the main Stage, but I cannot change it once it has been shown.
You also need to remove the background from the root of your new scene:
subwindow.setBackground(null);

how to lock the size of a Stage in java

I made a very simple java application with 3 scenes and 3 buttons.
button3 (b3) opens a popup but I set the image to lock size but the window can still be changed by using the mouse on one of the corners.
is there a way to lock the size so the curson can't edit this?
Here's the simplified code (just to get and idea)
public class Handler extends Application
{
#Override
public void start(Stage primaryStage) throws Exception {
Button button1 = new Button("AD");
VBox layout1 = new VBox(button1);
layout2.setAlignment(Pos.CENTER);
Pane layout2 = new Pane();
layout3.setStyle("-fx-background-image: url(ad.jpg);" +
"-fx-background-repeat: no-repeat;" +
"-fx-background-size: initial;");
Scene scene1 = new Scene(layout1);
Scene scene2 = new Scene(layout2);
//Stage (window)
primaryStage.setTitle("Basic Handler");
primaryStage.setScene(scene1);
primaryStage.setHeight(400);
primaryStage.setWidth(400);
primaryStage.show();
button1.setOnAction(event -> {
Stage PopUp = new Stage();
PopUp.setHeight(266);
PopUp.setWidth(474);
PopUp.setTitle("Sponsored AD");
PopUp.setScene(scene2);
PopUp.showAndWait();
});
}
}
Just add this in your stage :
PopUp.setResizable(false);

In Javafx when a window is opened how can i set it so that the user isnt able to open the same window again?

I tried a lot but just couldn't find any solution. At the moment the opened window(popup window) is always on top but the user can still access the main window. That's how it should be, but it shouldn't be possible to open the same popup window again.
Stage stage = new Stage();
stage.setTitle(panelTitle);
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.setAlwaysOnTop(true);
stage.showAndWait();
Thank you in advance!
As LazerBanana said, I would disable the button that opens the window, and I would enable it when you close it.
Stage stage = new Stage();
button.setDisable(true);
stage.setTitle(panelTitle);
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.setAlwaysOnTop(true);
stage.showAndWait();
// your logic here
button.setDisable(false);
An alternative solution to creating a new one each time is to create one and just setup and show.
public class Stack extends Application {
private final Stage popup = new Stage();
#Override
public void start(Stage stage) throws Exception {
BorderPane root = new BorderPane();
root.setPrefWidth(400);
root.setPrefHeight(200);
Button button = new Button("ClickMePopup");
root.setCenter(button);
button.setOnAction(
event -> {
if (!popup.isShowing()) {
// you dont set modality because after the stage is set to visible second time it will throw an exception.
// Again depends on what you need.
// popup.initModality(Modality.WINDOW_MODAL);
// this focuses the popup and main window is not clickable
// popup.initOwner(stage);
VBox dialogVbox = new VBox(20);
dialogVbox.getChildren().add(new Text("Some Dialog"));
Scene dialogScene = new Scene(dialogVbox, 300, 200);
popup.setScene(dialogScene);
// you can actually put all above into the method called initPopup() or whatever, do it once, and just show it here or just bind the property to the button.
popup.show();
}
});
Scene scene = new Scene(root);
stage.setTitle("Stack");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Or disable the button when clicked, but if your popup is not driven by the button or can be opened from other places the first idea would be a bit better in my opinion. Depends on what you need.
Or just create your own class and Springify it.

Show windows JavaFX

i new in JavaFX.
I have a problem, when a view, called another ... i can see items on the desktop.
in other words, to click a button on the a.fxml view, it closes and opens the b.fxml view.
but in this transition, i can see the desktop of windows.
is possible what this may not happen ??
Stage stage = new Stage();
stage.setFullScreen(true);
Pane myPane = null;
FXMLLoader fxmlLoader;
fxmlLoader = new FXMLLoader(getClass().getResource(rutaAbrir));
myPane = (Pane)fxmlLoader.load();
Scene scene = new Scene(myPane);
stage.setScene(scene);
General controller = (General) fxmlLoader.getController();
controller.setPrevStage(stage);
stage.show();
Thanks.

Switching scene in javaFX

I have problem when trying to close current scene and open up another scene when menuItem is selected. My main stage is coded as below:
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
Pane myPane = (Pane)FXMLLoader.load(getClass().getResource
("createProduct.fxml"));
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
Then within createProduct.fxml, when menuItem is onclick, it will perform this:
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
stage.show();
}
It does opened up createCategory.fxml. However, the previous panel which is createProduct.fxml does not close. I know there's something called stage.close() to do this but I have no idea where to implement it since I not passing the scene from main right from the start. I wonder how should I fix this.
Thanks in advance.
You have to make some changes in start method, like..
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("createProduct.fxml"));
Pane myPane = (Pane)myLoader.load();
CreateProductController controller = (CreateProductController) myLoader.getController();
controller.setPrevStage(primaryStage);
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
and your CreateProductController.java will be,
public class CreateProductController implements Initializable {
Stage prevStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show();
}
}
You have multiple ways to solve your problem, few of them I tell you. What I understood from your question is that you want to create an application which contains a navigation (menu bar) on the top and by using it user can switch to any screen.
The easiest method is to make your Navigation Bar available globally, either by you make it static or by passing it to screens, or by any other method. Second, make a single screen (say Border Pane) and load other screens on it (its center).
You can also use spring integration in project so that it provide you better injection of controllers. See Ref : http://www.zenjava.com/2011/10/25/views-within-views-controllers-within-controllers/
You can also create your own single screen controller and manage other screens using that.
See Ref: https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1
But for this (above) you have to change or update your current architecture.
you can get the current open window/stage from the fx:id . just make sure you have an item in your createProduct.fxml that has fx:id like below
<TextField fx:id="username" labelFloat="true" layoutX="80.0" layoutY="300.0" prefHeight="32.0" prefWidth="260.0" promptText="Login Username" />
Hope you see the fx:id attribute.
Then in your start controller, leave as it is
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
Pane myPane = (Pane)FXMLLoader.load(getClass().getResource("createProduct.fxml"));
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
Then in your CreateProductController.java
//make sure you import the javafx item you have used, eg have used TextField
import javafx.scene.control.TextField;
public class CreateProductController implements Initializable {
// make sure the variable has the same type as the item in the fxml "TextField" and same name "username"
#FXML
private TextField username;
//we use the above variable since its referencing the window to which it belongs because of the fx:id attribute, then we close it
private void closeStage()
{
((Stage) username.getScene().getWindow()).close();
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
closeStage();// we close the old stage
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
stage.show();
}
}

Categories

Resources