Reputation: 41
I m doing a small project which manipulates a Albums of a singer, so the project provides you to add new singer to add album for specific singer and to add songs to specific album for specific singer , i m trying to get the user the chance to get the singer albums so he doesn't have to type it with his hand so I used two ComboBox
one named "AvailableSinger" which clearly for available singer and the other "AlbumAvailable" for available albums for each singer, when the user select a singer the "AlbumAvailable" will display the albums of a singer selected in "AvailableSinger"
I've added two artists "Adele" and "Eminem" but when I choose Eminem the "Hello" album of "Adele" will only display.
package sample;
import MusicManiPulation.Album;
import MusicManiPulation.Singer;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import java.util.ArrayList;
public class AddSongsToAlbum {
@FXML
ComboBox<String> AvailableSinger ;
@FXML
ComboBox<Album> AlbumAvailble ;
public void initialize(){
AvailableSinger.getItems().addAll(Singer.getInstance().GetSingerNames());
AvailableSinger.getSelectionModel().selectFirst();
ArrayList<Album> AlbumList =Singer.getInstance().getAlbumNameoforSinger(AvailableSinger.getSelectionModel().getSelectedItem());
AlbumAvailble.getItems().addAll(AlbumList);
}
}
The Singer Class(in case you want to understand what I am doing)
package MusicManiPulation;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Singer {
public static Singer Instance = new Singer();
Map<String , ArrayList<Album>>AlbumListfoEachSinger ;
private Singer() {
this.AlbumListfoEachSinger = new HashMap<>();
}
public ArrayList<String> GetSingerNames(){
ArrayList<String>SingerName = new ArrayList<>();
SingerName.addAll(AlbumListfoEachSinger.keySet());
return SingerName;
}
public Map<String, ArrayList<Album>> getAlbumListfoEachSinger() {
return AlbumListfoEachSinger;
}
public ArrayList<Album> getAlbumNameoforSinger(String SingerName){
return AlbumListfoEachSinger.get(SingerName);
}
public static Singer getInstance() {
return Instance;
}
}
As you can see only the Adele albums display but when selecting another Artist it doesn't work.
Upvotes: 0
Views: 49
Reputation: 4258
First, you have to pay more attention to naming conventions in Java. Check this resource.
You can solve your problem by using one of the following methods:
Listeners can be added to JavaFX properties, you can add a listener that will fill the albums for a specific singer when a new singer is selected. Add this listener in your initialize method:
availableSingers.valueProperty().addListener((observable, oldSelectedSinger, newSelectedSinger) -> {
List<Album> albums = Singer.getInstance().getAlbumNameoforSinger(newSelectedSinger);
availableAlbums.getItems().setAll(albums);
});
you can bind the items of the albums combo box to the selected value of the singer's combo box. Add this listener in your initialize method:
availableAlbums.itemsProperty().bind(Bindings.createObjectBinding(() -> {
List<Album> albums = Singer.getInstance().getAlbumNameoforSinger(availableSingers.getValue());
return FXCollections.observableArrayList(albums);
}, availableSingers.valueProperty()));
Upvotes: 2