0

I'm quite new to JavaFX. I'm trying to pass the data or items i got in observable list in Scene1 to Scene2

Window1 just List the files from a folder. I also created a data model (Cousin says it's called bean) anyway, its Table_files which defines the data I would like to save. Currently its just fileID and Filename

**MAIN CONTROLLER or WIndow1**
public class FXML_ListFilesController implements Initializable {
private final String directory = "/Users/ellen/Desktop/test 1";

// COLLECTIONS
private ObservableList<String> results = FXCollections.observableArrayList();

@FXML
private ListView<String> list = new ListView<String>(results);
@FXML
private TextField txt1;
@FXML
private TextField txt2;
@FXML
private Label lblDirectory;
@FXML
private Button btnOpenWindow;

//COLLECTIONS
ObservableList<Table_Files> files_data = FXCollections.observableArrayList();

@FXML
private void handleListMouseClick(MouseEvent event) {
    System.out.println(list.getSelectionModel().getSelectedItem());
}

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


      File[] files = new File(directory).listFiles();
        for (File file : files) {
            if (file.isFile()) {
            results.add(file.getName());
            }
        }
        list.setItems(results); // ADD TO LIST

        lblDirectory.setText(directory);

}

@FXML
private void handleOpenWindow(ActionEvent event) {
    try {
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("FXML_NewWindow.fxml"));
    Scene scene = new Scene(fxmlLoader.load(), 400, 200);
    Stage stage = new Stage();
    stage.setTitle("New Window");
    stage.setScene(scene);
    stage.show();
} catch (IOException e) {
    Logger logger = Logger.getLogger(getClass().getName());
    logger.log(Level.SEVERE, "Failed to create new Window.", e);
}
}

// Getter
public String getDir(){
    return directory;
}

@FXML
private void handleSaveToBean(ActionEvent event) {

    Table_Files file_entry;
    for (int i = 0 ; i < list.getItems().size() ; i++){

        file_entry = new Table_Files(i, list.getItems().get(i));
        files_data.add(file_entry);
    }

    for(Table_Files o:files_data){
        System.out.println("Testing: " + o.getFilename());
    }

}

}

I would like to access the files_data from a different window. I was able to do it by using static. static ObservableList<Table_Files> files_data = FXCollections.observableArrayList();

and a getter

public ObservableList<Table_Files> getCollections(){
        return files_data;
    }

is there a way to get this without having it as a static?

Please help.

CuteGirlyGeek
  • 25
  • 1
  • 7
  • Just pass the list to the controller for `FXML_NewWindow.fxml` after loading the new FXML file. See https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310 – James_D Jul 19 '17 at 13:36
  • THank you James_D.. I found the post yesterday and been trying it out with no success... I wasn't really reading everything.. But now, with your additional comment it really made me think and tried it out. and YAY!!! it worked!!! Thank you!!! – CuteGirlyGeek Jul 20 '17 at 02:45

0 Answers0