0

I am trying to write code for the Save and Save As menuItems in my javafx Application.

I have already written my Save As menuItem.

menuItemSaveAs.setOnAction(new EventHandler<ActionEvent>(){
     // run the saveAs(); method     
     @Override
     public void handle(final ActionEvent e){
          saveAs();
     }
})

However, when it comes to writing the Save menuItem, I need to check if the current workflow is associated with a file already, if it is, then just save current workflow to the current file. Otherwise, perform the saveAs(), whereby the save dialogue is displayed and user has to type in the file name and then save. I have code written for saveAs() already.

It is the checking if the current workflow is associated with a file already part that I do not feel confident.

At the moment, my naive way of checking is:

public void save(){
     if(file != null){    // <-  not sure if this is the right check. 
          // write objects to file
     } else{
          saveAs();
     }
} 

Since I have other menuItems New, Save, Save As , Open and Close in my application. Using this approach, I have to make sure FileInputStream and FileOutputStream are never close in the first 4 menuItems, i.e. New, Save, Save As , Open. And they can only be closed if the Close menuItem is clicked. Is this the correct approach?

Are there any other better ways to handle this ?

mynameisJEFF
  • 4,073
  • 9
  • 50
  • 96

1 Answers1

0

mynameisJEFF, Your approach is correct. After you picked the file using save as, remaining operations is common for both save and save as.

Janath
  • 137
  • 9