I am trying to load a new FXML when clicking on item of a treeview.
Well, There is a part of MainApp, where FXML are loaded. Here I have implemented a root view in which other views will be loaded.
I try to load clientView.fxml when someone clicks on the node "voir mes clients" on treeview. The view will be wrapped in the view loaded by menuView.fxml in function mainMenuView
In class TreeLoadingEventHandler, function handle load the treeview implemented in the controller and finally the treeview is loaded in the function mainMenuView.
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootView;
private static AnchorPane menuView;
public MainApp()
{}
@Override
public void start(Stage primaryStage) {
this.primaryStage=primaryStage;
this.primaryStage.setTitle("MaK@mpta");
launchRootLayout();
mainMenuView();
}
public void launchRootLayout()
{
try {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/rootView.fxml"));
rootView = (BorderPane)loader.load();
Scene scene = new Scene(rootView);
//scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void mainMenuView()
{
try
{
FXMLLoader loader=new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/menuView.fxml"));
menuView=(AnchorPane)loader.load();
rootView.setCenter(menuView);
MenuViewController controller = loader.getController();
controller.setMainApp(this);
//controller.loadItems();
TreeLoadingEventHandler Tree=new TreeLoadingEventHandler(controller);
Tree.handle(null);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void showClient()
{
try
{
FXMLLoader loader=new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/clientView.fxml"));
AnchorPane clientView=(AnchorPane)loader.load();
AnchorPane.setLeftAnchor(clientView, 220.0);
menuView.getChildren().add(clientView);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private class TreeLoadingEventHandler implements EventHandler<ActionEvent> {
private MenuViewController controller;
TreeLoadingEventHandler(MenuViewController controller) {
this.controller = controller;
}
@Override
public void handle(ActionEvent t) {
controller.loadItems();
}
}
}
Here is the java class which implements the controller function setMainApp retrieves the reference of controller from which I can call the function showClient of the MainApp. I have implemented a cell factory setfactoryCell to invoke the loading of clientView.fxml when someone clicks on item "voir mes clients".
public class MenuViewController {
@FXML
TreeView <String> treeview;
// Reference to the main application.
private MainApp mainApp;
// public MenuViewController()
@SuppressWarnings("unchecked")
public void initialize(URL location, ResourceBundle resources)
{
loadItems();
}
@SuppressWarnings("unchecked")
public void loadItems()
{
TreeItem<String>root=new TreeItem<>("Root");
TreeItem<String> clients=new TreeItem<>("Clients");
TreeItem<String> addClient=new TreeItem<>("Ajouter Client");
TreeItem<String> seeClient=new TreeItem<>("Voir mes clients");
TreeItem<String> articles=new TreeItem<>("Articles");
TreeItem<String> devis=new TreeItem<>("Devis");
root.getChildren().addAll(clients,articles,devis);
clients.getChildren().addAll(addClient,seeClient);
root.setExpanded(true);
treeview.setRoot(root);
treeview.setShowRoot(false);
setfactoryCell();
}
private void setfactoryCell()
{
treeview.setCellFactory(tree -> {
TreeCell<String> cell = new TreeCell<String>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty) ;
if (empty) {
setText(null);
} else {
setText(item);
}
}
};
cell.setOnMouseClicked(event -> {
if (! cell.isEmpty()) {
TreeItem<String> treeItem = cell.getTreeItem();
// do whatever you need with the treeItem...
if (treeItem.getValue().equals("Voir mes clients"))
{
selectViewClient();
}
}
});
return cell ;
});
}
/**
* Is called by the main application to give a reference back to itself.
*
* @param mainApp
*/
public void selectViewClient()
{
mainApp.showClient();
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
This thing doesn't work.
Even The treeview which worked before , doesn't work anymore.
Could I have a help on this please ?
How to do this working when I click on the item "voir mes clients" ?
Sorry for posting so much code but I don't see here how to be more short.
Thank you