I have a stage with these properties
stage = new Stage(StageStyle.DECORATED);
stage.setFullScreen(true);
stage.setFullScreenExitHint("");
stage.setTitle("My App");
stage.getIcons().add(new Image(icon-address));
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("start.fxml"));
fxmlLoader.setController(this);
...
...//and other properties
It is in full screen in this stage. I have a button and each time I click on it , a method of another class is called and the reference of this stage is passed to it.
For example, if I have two classes Start and ButtonClicked
In Start, I have this :
Button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
buttonClicked a = new buttonClicked ();
try {
a.render(stage);
} catch (Exception ex) {
Logger.getLogger(menuController.class.getName())
.log(Level.SEVERE, null , ex);
}
}
});
Now, the render method is called and I have the following code inside render :
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("buttonclicked.fxml"));
fxmlLoader.setController(this);//set Controller for our fxml file
loader = fxmlLoader.load();//loader is a stackPane because buttonclick.fxml has stackPane
stage.setScene(new Scene(loader));
stage.setFullScreen(true);
...
The problem is, if user clicks on button, stage become a 1024x700(because i have pictures in start.fxml and buttonclicked.fxml. The size of both the picture is 1024x700
After a moment, it becomes full screen. How can i fix this ?
I want to kill the delay in full screen
Thanks
Try
stage.getScene().setRoot(loader);
in render method.
By doing this, the creation of a new scene is eliminated. Therefore I guess, some UI related updates and calculations that must be done after the scene has been shown, are also eliminated.
Related
Hello I am having an JavaFX app with few controllers and I have 2 options to get back to previous screen. User can click button 'leave' or after finishing some tasks on this screen will be moved to previous automatically. I have problem here because I've created method wiut fxml annotation which takes ActionEvent object as parameter and is called when user click button and when user will finish tasks and should be moved automatically to previous screen I cannot call this method because I dont this object, it's created when an action - click in this case is made. How can I make it possible for both 'exit' options?
So here is my method which is used 'onAction' for my button:
#FXML
private void leaveRoomAction(ActionEvent event) {
try {
removePlayerFromRoom();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("LobbyView.fxml"));
Parent root = (Parent) loader.load();
LobbyController lobbyController = (LobbyController)loader.getController();
lobbyController.setClientThread(client);
lobbyController.setNameAndBalance(client.getPlayer().getName());
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
And later in other part of programe:
if(isFinished()){
//here I want write leaving this screen and getting back to previous
}
First, find another way to get a reference to the Stage. Since you almost certainly have a reference to some node in the scene in your controller, you can replace
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
with
Stage stage = (Stage) anyNode.getScene().getWindow();
where anyNode is just something you have injected into the controller.
Now you don't need the parameter at all, so you can just remove it. I.e. you end up with
#FXML
private Node anyNode ; // probably a more specific type than Node.
#FXML
private void leaveRoomAction() {
try {
removePlayerFromRoom();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("LobbyView.fxml"));
Parent root = (Parent) loader.load();
LobbyController lobbyController = (LobbyController)loader.getController();
lobbyController.setClientThread(client);
lobbyController.setNameAndBalance(client.getPlayer().getName());
Scene scene = new Scene(root);
Stage stage = anyNode.getScene().getWindow();
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
And now you can just call the method:
if ( isFinished() ) {
leaveRoomAction()
}
I have a project in school where I have to develop a program where you first can choose whether you want to save/read to/from a SQL DB or to save/read to/from XML.
I've made a GUI where you can choose between both methods.
The GUI closes after the user clicks on one of the buttons and the MainMenu GUI opens.
Now I need to know in the MainMenuController what the user choose.
I found online a Method to call the MainMenuController inside the first controller, with FXMLLoader.getController().
try {
Stage stage = new Stage();
FXMLLoader Loader = new FXMLLoader();
Parent root = Loader.load(getClass().getResource("MainMenu.fxml"));
MainMenuController mc = Loader.getController();
mc.setSave("sql");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
e.printStackTrace();
}
MainMenuController
public class MainMenuController {
private String save = null;
public void setSave(String save) {
this.save=save;
}
public String getSave() {
return save;
}
}
But when i try to access a method in MainMenuController I get a NullPointerException for
mc.setSave("sql")
First ,to understand this issue ,you should make some tricks to detect where is your problem.When you do :
System.out.println(mc);
You will find the result is null.So you can not call setSave("sql") with null object,you got a null controller because your did not specify the location of your file,but you can change some lines to resolve your problem :
try {
Stage stage = new Stage();
FXMLLoader fxm = new FXMLLoader(getClass().getResource("MainMenu.fxml"));
Parent parent = (Parent) fxm.load();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
FirstController mc = fxm.getController();
System.out.println(mc);
mc.setSave("sql");
} catch (Exception e) {
e.printStackTrace();
}
You are calling the static method FXMLLoader.load(URL). Because this is a static method, it has no effect on the state of the FXMLLoader instance you created; specifically the controller field is not set.
Instead, set the location on the FXMLLoader instance, and call the instance method load():
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("MainMenu.fxml"));
// or just FXMLLoader loader = new FXMLLoader(getClass().getResource("MainMenu.fxml"));
Parent root = loader.load();
I have a controller that opens a new stage as a popup:
#FXML
private void onClickPayments(ActionEvent event) throws IOException{
FXMLLoader loader = new FXMLLoader(getClass().getResource("ClientPayments.fxml"));
Parent root = (Parent) loader.load();
ClientPaymentsController controller = (ClientPaymentsController) loader.getController();
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL);
stage.setResizable(false);
stage.setOnCloseRequest((WindowEvent we) -> {
clientBLL.retrieve(clientID);
updateWarning();
});
stage.show();
controller.setClientID(clientID);
}
When the other stage is closed by the 'x' button the 'stage.setOnCloseRequest' is executed. But in that stage I have a button to close:
#FXML
private void onClickExit(ActionEvent event){
((Stage) ((Node) event.getSource()).getScene().getWindow()).close();
}
It closes, but the method 'stage.setOnCloseRequest' isn't being executed.
Is this method wrong or is there a way to close a popup in a way that acts exactly like the 'x' button in the window?
The setOnCloseRequest handler is executed when there is an external request to close the window (i.e. not one from your code). See the documentation.
If it's enough to execute that logic immediately after the window is closed, just use the regular onHidden handler instead:
stage.setOnHidden((WindowEvent we) -> {
clientBLL.retrieve(clientID);
updateWarning();
});
If you really need to use the onCloseRequest handler (e.g. because you might want to veto the close), just move your close logic to a method and invoke it from both types of request to close the window.
public void doClose() {
clientBLL.retrieve(clientID);
updateWarning();
}
#FXML
private void onClickExit(ActionEvent event){
doClose();
((Node) event.getSource()).getScene().getWindow().hide();
}
and
stage.setOnCloseRequest((WindowEvent we) -> {
controller.doClose();
});
So here is the problem I am having: I have 3 button, when I press one of the 3 buttons the new scene will display. Yet I cant seem to figure out how to make different panes appear visible depending on which button is pressed. what is the best method to perform this action? How do i use Id's from different scene controllers in order to change the properties of the pane within the main scene button listener?
currently on my main controller when each button is released the below action listener executes and displays secondscreen.fxml. the secondscreen.fxml has 2 different panes, depending on which button is pressed I need 1 of the 2 pains to set to visable.
#FXML
public void handleButtonAction(MouseEvent event) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("secondscreen.fxml"));
Stage stage = new Stage();
stage.setTitle("title");
stage.setScene(new Scene(root));
stage.show();
((Node)(event.getSource())).getScene().getWindow();
} catch (Exception e) {
e.printStackTrace();
}
}
After you load a view, you can access its controler.
Keep in mind, that your controller must be asigned to your fxml file fx:controller="your.package.SecondScreenController.java"
FXMLLoader loader = new FXMLLoader.load(getClass().getResource("secondscreen.fxml"));
// load view
Parent root = loader.load();
// after view is loaded, access its controller
SecondScreenController controller = (SecondScreenController) loader.getController();
// modify view using methods on your controller
controller.setTabIndex(0)
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
I am sorry to ask this silly question. I am unable to solve it and I need guidance.
In my application I start my main java code with menu as
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("MONC GUI :: Abhijit Bhattacharyya");
FXMLLoader loader = new FXMLLoader(MoncGUI.class.getResource("RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
//Scene scene = new Scene(rootLayout);
rootScene = new Scene(rootLayout);
primaryStage.setScene(rootScene);
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
primaryStage.show();
}
......
Then I opt to go to a particular menu page by opening another fxml in the main class as
public void GeomStart() {
try {
FXMLLoader loader = new FXMLLoader(MoncGUI.class.getResource("Geom.fxml"));
AnchorPane geomPage = (AnchorPane)loader.load();
rootLayout.setCenter(geomPage);
GeomController controller = loader.getController();
controller.setMyScene(rootScene);
} catch (IOException ex) {
System.out.println(" Problem in loading geometry set");
Logger.getLogger(MoncGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
The geom.fxml is associated with geomController.java which puts a primitive geometric form on the screen and I want it to be rotated. I find mouseHandler code in the internet which should be activated as screen is passed.
I am confused which screen should I pass. Is it the root screen initiated by the main class at the start or I need to define a new screen locally here in this controller. I tried with a screen local to this controller yet the code handle is not passed to MouseHandler code.
Thanks and Regards
There is no need to pass the scene arround at all. Inside your controller you have access to the nodes which you have defined in your FXML file. Once this file is loaded and you have attached its root node to the scene graph you can get the scene by calling the .getScene() method of any of these nodes.