如何从另一个控制器操纵TableView?

时间:2019-06-26 08:24:57

标签: java javafx tableview

注意:AlbumAvailble是一个ComboBox,其中包含歌手的专辑。      :AvailableSinger和ComboBox可以容纳歌手。

我正试图在另一个对话框“ DisplaySongs.fxml”中的TableView中显示专辑的歌曲。

我试图在“ DisplaySongs.fxml”的“ DisplaySongs.java”控制器中创建一个方法,以将所有专辑歌曲添加到表中。所以在显示对话框的方法中,我已经传递了AlbumAvailble的selectedItem并获取了它的歌曲列表。

主窗口控制器:

@FXML
public void Display() {
    Dialog<ButtonType>DisplaySong = new Dialog<>();
    DisplaySong.initOwner(DisplayBorder.getScene().getWindow());
    DisplaySong.setTitle("DisPlay Songs");
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("DisplaySongs.fxml"));
    try {
        DisplaySong.getDialogPane().setContent(fxmlLoader.load());
    }catch (IOException E){
        E.getStackTrace();
    }
    DisplaySong.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
    Optional<ButtonType> result = DisplaySong.showAndWait();
    if(result.isPresent()) {
        DisplaySongs controller = fxmlLoader.getController();
        controller.Display(AlbumAvailble.getSelectionModel().getSelectedItem());
    }
}

歌曲显示控制器:

public class DisplaySongs {

    @FXML
    private TableView<Song> Songs ;

    public void Display(Album Alb) {
        Songs.getItems().addAll(Alb.getSongsList());
    }
}

歌曲课:

package MusicManiPulation;

import javafx.beans.property.SimpleStringProperty;

import java.time.LocalDate;

public class Song {

    private SimpleStringProperty SongName = new SimpleStringProperty("") ;
    private SimpleStringProperty SongLength = new SimpleStringProperty("");
    private LocalDate ReleasedDay ;

    public Song(String songName, String songLength, LocalDate releasedDay) {
        SongName.set(songName);
        SongLength.set(songLength);
        ReleasedDay = releasedDay;
    }

    public String getSongName() {
        return SongName.get();
    }

    public void setSongName(String songName) {
        SongName.set(songName);
    }

    public String getSongLength() {
        return SongLength.get();
    }

    public void setSongLength(String songLength) {
        SongLength.set(songLength);
    }

    public LocalDate getReleasedDay() {
        return ReleasedDay;
    }

    public void setReleasedDay(LocalDate releasedDay) {
        ReleasedDay = releasedDay;
    }
}

相册类:

package MusicManiPulation;

import java.time.LocalDate;
import java.util.ArrayList;

public class Album {

    private String AlbumNam ;

    ArrayList<Song> SongsList ;

    public Album(String albumNam) {
        AlbumNam = albumNam;
        this.SongsList = new ArrayList<>();
    }

    public boolean addNewSongToAlbum(String SongName , String SongLength , LocalDate ReleadsedDay) {

        boolean song = findSong(SongName);

        if (song) {
            return false;
        }
        SongsList.add(new Song(SongName , SongLength,ReleadsedDay));
        return true;
    }


    public boolean removeSong(String SongName){
        for(Song song :SongsList){
            if(song.getSongName().equalsIgnoreCase(SongName)){
                SongsList.remove(song);
                return true;
            }
        }
        return false;
    }

    private boolean findSong(String SongName){

        for(Song song:SongsList){
            if(song.getSongName().equalsIgnoreCase(SongName)){
                return true;
           }
        }
        return false;
    }

    public String getAlbumNam() {
        return AlbumNam;
    }

    public ArrayList<Song> getSongsList() {
        return SongsList;
    }

    @Override
    public String toString() {
        return AlbumNam;
    }
}

专辑类中的getSongList方法:

public ArrayList<Song> getSongsList() {
    return SongsList;
}

每次我按下“显示”按钮时表都是空的

1 个答案:

答案 0 :(得分:1)

public void Display(){

       Dialog<ButtonType>DisplaySong = new Dialog<>();
        DisplaySong.initOwner(DisplayBorder.getScene().getWindow());
        DisplaySong.setTitle("DisPlay Songs");
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("DisplaySongs.fxml"));
        try {
            DisplaySong.getDialogPane().setContent(fxmlLoader.load());
        }catch (IOException E){
            E.getStackTrace();
        }
        DisplaySong.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
        System.out.println("BEFORE Show");
        //Optional<ButtonType> result = DisplaySong.showAndWait(); //removed this line too
        DisplaySong.show();

      // if(result.isPresent()){ REMOVED THIS LINE.
     // YOU FORGOT THE CAST HERE :
         DisplaySongs controller = (DisplaySongs) fxmlLoader.getController(); 
            controller.Display(AlbumAvailble.getSelectionModel().getSelectedItem());

}