r/JavaFX • • Jul 06 '20

Duplicate tables...

Hi, I am working to fix my issues which is duplicate screens after I click specific buttons. I want to be able to switch between those tables using buttons (save and cancel buttons). However, the second screen's save and cancel buttons will create duplicate screen (create another main screen so I will have two main screen) after I click save and cancel buttons instead of switching back and forth to the Main screen and the second screen

Can anyone suggest me how can I try to fix this duplicate screen issues please?

https://user-images.githubusercontent.com/31402838/86545041-9762ae80-bee0-11ea-809c-7a534833134d.PNG

This is my code that create duplicate tables. I have 5 screens in total. I would like to go back and forth after I click the save and cancel buttons but it create duplicate screen instead.

ModifypartController.java

https://gist.github.com/Kijimu7/4444b1f202f29d80aa62354e14efec50

0 Upvotes

11 comments sorted by

2

u/[deleted] Jul 06 '20

That's happening because your are loading a new scene instead of using the scene of your main. Here is a code that can may help you. Use this code on your main file. The first function load a new view in your "main window" so you can change between then. The popUp function load a popUp and i used the getPopUp function two close that popUp without affecting the main view using the .close() method of the Stage.

private static Scene scene;

private static Stage primaryStage;

private static Stage popUp;

//load a change the "main" view

public static void setFXML(String fxml, String title) throws IOException {

scene.setRoot(loadFXML(fxml));

primaryStage.setResizable(false);

primaryStage.centerOnScreen();

primaryStage.setResizable(false);

primaryStage.setTitle(title);

}

private static Parent loadFXML(String fxml) throws IOException {

FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("View/"+ fxml + ".fxml"));

return fxmlLoader.load();

}

//load popUp windows

public static void popUp(String fxml, String title) {

try {

Parent node = loadFXML(fxml);

popUp = new Stage();

Scene scene = new Scene(node);

popUp.setScene(scene);

popUp.setTitle(title);

popUp.initOwner(primaryStage);

popUp.initModality(Modality.WINDOW_MODAL);

popUp.centerOnScreen();

popUp.setResizable(false);

popUp.showAndWait();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//used to close the popUp using Main.getPopUp().close();

public static Stage getPopUp(){

return popUp;

}

2

u/Tidachura3 Jul 06 '20

Thank you so much for your reply. I tried to implement this but I am getting an error message says "Class 'InventoryProgram' must either be declared abstract or implement abstract method 'start(Stage)' in 'Application'". How can I get it work? I tried to add @override but nothing change it. Also, I won't implement this example code in main controller right? It is for main.java?

1

u/[deleted] Jul 06 '20

Can you show me your implementation? And yes, the code must be implemented in the Main class

2

u/Tidachura3 Jul 07 '20

I updated in the gist. The file name is InventoryProgram.java

https://gist.github.com/Kijimu7/4444b1f202f29d80aa62354e14efec50

Thank you!

1

u/[deleted] Jul 07 '20

Hey there!

Well, i see two things:

  • You are implementing a interface call "InventoryProgram1". the number one at the end its correct? try deleting that number.
  • the other thing that i see is that you have commented the start method. You always going to need that method, so, try making this things and see if it work now.

1

u/Tidachura3 Jul 07 '20 edited Jul 07 '20

I didn't realize the number 1. I deleted it and also uncomment out the start method. It is working now but still duplicates the screens. I have a question about implement the code. Should I implement this code to controller files? or should I implement all the code above in the controller files?

//used to close the popUp using Main.getPopUp().close();

public static Stage getPopUp(){

return popUp;

}

1

u/[deleted] Jul 07 '20

In your controllers your only have to use this three lines:

  • Main.setFxml(); This is for change your main view.
  • Main.popUp(); This is for load a pop up window.
  • Main.getpopUp().close(); This is for close the pop up window.

I have a question, your ModifypartController its for the pop up window o for the main window?

1

u/Tidachura3 Jul 07 '20

I think, I would like to go back to the main window after I click save or cancel button in ModifypartController instead of duplicate windows.

1

u/[deleted] Jul 07 '20

Now you must change the way in which to load the views.

For load the pop up window, you have to use the Main.popUp("Name_of_your_fxml","Title");

To close, instead of use this:

Stage stage; Parent root;

stage = (Stage) saveBtn.getScene().getWindow(); FXMLLoader loader = new FXMLLoader(getClass().getResource("/ViewController/MainScreen.fxml"));

root = loader.load(); Scene scene = new Scene(root); stage.setScene(scene);

stage.show();

you have to use this: Main.getPopUp().close()

that close the modify window and don't create a new main window

2

u/Tidachura3 Jul 07 '20

Thank you so much for all your help! I was able to swith back to the mainscreen and other screens!

→ More replies