1

here is my problem :

I have 3 FXML files with each controller assigned. ExplorerPage.FXML : layout used to show a gridpane with undefined object number inside. ExplorerNoteController.FXML : the object (StackPane) inserted in the gridpane. NoteHoverMenuController.FXML : When the user moves the mouse on the previous object it shows options to update or delete the object.

To resume :

ExplorerPage.FXML -> ExplorerNoteController.FXML -> NoteHoverMenuController.FXML

Gridpane -> Stackpane -> Borderpane(2)

What I want to do is when I click on the "delete" button in the NoteHoverMenuController.FXML, this remove the current object from the gridpane (located in ExplorerPage.FXML). But I have no clue to do this. I tried binding with properties, static methods, .. but no satisfaying results.

This is my code :

ExplorerPage controller

public class ExplorerPage implements Initializable {

private Connection conn;

// LOAD ALL NOTES
private List<Note> gridNotes = new LinkedList<Note>();

JFXRadioButton alertButton = new JFXRadioButton(); // TEMP
StackPane spaneExpNote; // StackPane used to switch front/back pane (overlay)
private byte j = 0;
private byte k = 0;

@FXML
private GridPane FXMLNotesPane = new GridPane();


@Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
    try {
        // LOAD DATAS FROM DB W/ PARAM FOLDER
        conn = DBInitialize.getInstance();
        DAO<Note> findAllNotes = new NoteDAO(conn);
        gridNotes = findAllNotes.findAll("Orange");

        // STACKPANES CONSTRUCTION

        for(int i = 0; i < gridNotes.size(); i++)
        {
            if (j>2) {
                j=0;
                k++;
            }
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fr/cryption/view/ExplorerNote.fxml"));
            spaneExpNote = (StackPane) loader.load();
            ExplorerNoteController controller = loader.getController();
            controller.setData(gridNotes.get(i).getTitleNote(), gridNotes.get(i).getContentNote(), gridNotes.get(i).getDateNote());
            FXMLNotesPane.add(spaneExpNote,j,k);
            j++;
        }

ExplorerNoteController

public class ExplorerNoteController implements Initializable {

@FXML
StackPane rootNotePane;
@FXML
private Label labPaneTitle1;
@FXML
private Label labPaneDate1;
@FXML
private Label labPaneContent1;

BorderPane pane = null;
String ok = "ok";

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

}   

public void setData(String title, String content, String date) {
    labPaneTitle1.setText(title);
    labPaneContent1.setText(content);
    labPaneDate1.setText(date);
}

public void showHoverMenu() throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fr/cryption/view/NoteHoverMenu.fxml"));
    pane = (BorderPane) loader.load();
    NoteHoverMenuController controller = loader.getController();
    rootNotePane.getChildren().add(pane);
}
public void hideHoverMenu() throws IOException {
    rootNotePane.getChildren().remove(pane);
}

....

NoteHoverMenuController

public class NoteHoverMenuController implements Initializable {

String idNote = "";

@FXML
BorderPane hovernode;

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

public void setID(String id) {
    idNote = id;
}

public void deleteNoteExplorer(ActionEvent event) {
    System.out.println(hovernode.getParent().lookup("stckpane")); // DELETE THE NOTE
}
}

I will update this post if you want FXML(s) as well. Thanks.

SCREENS : Screen Gridpane

Screen Hover StackPane

mtnp
  • 314
  • 7
  • 24
  • You can use [this idea](https://stackoverflow.com/questions/26653562/how-to-access-parent-member-controller-from-child-controller), twice (pass the reference to the `ExplorerPageController` to the `ExplorerNoteController`, and then pass it from there to the `NoteHoverMenuController`). Creating a model class using JavaFX properties, sharing an instance of that class with all the controllers, and binding or listening to the properties might be an easier way to go. – James_D Jan 23 '18 at 13:30
  • Thank you @James_D ! I will dig this solution and post again if I can't solve the problem. – mtnp Jan 23 '18 at 13:50
  • Sorry but finally, it doesn't work I'm totally desperate. I tried everything. The problem is I can't use : fx:include in my ExplorerPage.fxml because I add nodes dynamically in the controller. And binding doesn't work from a controller to another class. – mtnp Jan 23 '18 at 21:28
  • The approach should work fine: if your implementation of it isn't working you must have something wrong. I suggest you create a new project with the simplest possible, but complete, example of what you're trying to do and post the complete thing. (Ie create a [MCVE].) – James_D Jan 23 '18 at 21:39

0 Answers0