0

I have two fxml files with two controller classes MainController and PlaylistController. Each class has a TableView, table and nTable respectively.

table is displayed as the default tab on a TabPane

nTable aplies to a new tab created with a button.

What I want is to be able to select items in the default table and add them to a new table on a new tab in a single action (I can do it using two buttons, one of them being clicked after the new tab is created).

I'm guessing I need to make sure the program waits for the new tab to be created before setting the items but I don't know how to do that.

This is the method I'm using:

@FXML
PlaylistController pc;    

public static ObservableList<Track> newTracklist = FXCollections.observableArrayList();

public void addToNewPlaylist(){
    newTracklist.clear();
    newTracklist.addAll(table.getSelectionModel().getSelectedItems());

    Tab nTab = new Tab();

    FXMLLoader nLoader = new FXMLLoader(getClass().getResource("/fxml/fxPlaylist.fxml"));
    try {
        Parent nRoot = nLoader.load();
        nTab.setContent(nRoot);     
    } catch (IOException e) {
        e.printStackTrace();
    }
    tabPane.getTabs().add(nTab);
    nTab.isClosable();
    pc.nTable.setItems(newTracklist);
}
Peralta
  • 180
  • 1
  • 12

1 Answers1

0

Unless you are defining a constant, or something similar, static fields have no place in a controller.

Define a method in your PlaylistController to take a list of Tracks for display in its table:

public class PlaylistController {

    @FXML
    private TableView<Track> nTable ;

    // ...

    public void setTracks(List<Track> tracks) {
        nTable.getItems().setAll(tracks);
    }

    // ...
}

Then just call it when you load the FXML:

public void addToNewPlaylist(){

    Tab nTab = new Tab();

    FXMLLoader nLoader = new FXMLLoader(getClass().getResource("/fxml/fxPlaylist.fxml"));
    try {
        Parent nRoot = nLoader.load();
        PlaylistController controller = nLoader.getController();
        controller.setTracks(table.getSelectionModel().getSelectedItems());
        nTab.setContent(nRoot);     
    } catch (IOException e) {
        e.printStackTrace();
    }
    tabPane.getTabs().add(nTab);
}
James_D
  • 201,275
  • 16
  • 291
  • 322