简体   繁体   中英

iteration and adding to a observableArrayList - java

JFileChooser fileChooser = new JFileChooser();

    FileFilter ft = new FileNameExtensionFilter("MP3 Files", "mp3");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(ft);

        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
            String pathScannedOnLoad1 = fileChooser.getSelectedFile().toString();
            final MusicMediaCollection collection = MUSIC_SERVICE
            .createMusicMediaCollection(Paths.get(pathScannedOnLoad1));
    dataForTableView = FXCollections.observableArrayList(collection.getMusic());
    dataForTableView.addListener(makeChangeListener(collection));
    tableView.setItems(dataForTableView);
    tableView.setEditable(true); 
        }

This is my code to open a file and add it to a javaFX table, my question is, how would i change this code so it would open a jfilechooser and then when the user selects their content it ADDS to the table. Right now, if i try open a new file whilst content is already in the table, it will first delete whatever is in the table then add the new content.

thank you,

That class ObservableList has a nice method addAll() .

It works very much like "ordinary" java (non fx) collections.

In other words: in your current code, you simply create a completely new collection. Instead of doing that, you could call addAll() on an existing collection to add the newly created one; like:

if (dataForTableView == null) { 
  dataForTableView = FXCollections.observableArrayList(...
} else {
  dataForTableView.addAll(FXCollections.observableArrayList(...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM