JavaFX:将集合数据(observableList)从一个窗口传递到另一个窗口

时间:2017-07-19 06:14:25

标签: javafx

我对JavaFX很陌生。我试图将Scene1中可观察列表中的数据或项目传递给Scene2

Window1 just列出文件夹中的文件。我还创建了一个数据模型(Cousin称它为bean),它的Table_files定义了我想要保存的数据。目前它只是fileID和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());
    }

}

}

我想从另一个窗口访问files_data。我能够通过使用静态来做到这一点。 static ObservableList<Table_Files> files_data = FXCollections.observableArrayList();

和一个吸气剂

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

有没有办法在不将它作为静态的情况下得到它?

请帮忙。

0 个答案:

没有答案