I'm trying to create a JComboBox that will hold Playlist objects in it. I want to have it hold the Playlist's String title, which is the key/display value, and a reference to the actual Playlist so it can be directly accessed when a user clicks a button relating to the selected Playlist. The contents of the combo box will be dynamically changed by the user so someone in another question suggested using the DefaultComboBoxModel to allow it to update itself. I saw this question Adding items to a JComboBox recommended wrapping the item in a new class. I tried this but now my combo box comes up blank.
My old code:
String[] playlistsStringArray = {"Library"};
JComboBox playlists = new JComboBox(playlistsStringArray);
DefaultComboBoxModel model;
// worked but only held strings. I was having trouble referencing the playlist itself
model = new DefaultComboBoxModel(playlistsStringArray);
playlists.setModel(model);
My current code:
ComboItem myLibCombo = new ComboItem("Library", myLibrary);
DefaultComboBoxModel model;
ComboItem[] comboItems = new ComboItem[0];
JComboBox comboPlaylists = new JComboBox(comboItems);
// empty combo box
model = new DefaultComboBoxModel(comboItems);
comboPlaylists.setModel(model);
comboPlaylists.addItem(myLibCombo);
Also tried:
ComboItem myLibCombo = new ComboItem("Library", myLibrary);
DefaultComboBoxModel model;
ComboItem[] comboItems = {myLibCombo};
JComboBox comboPlaylists = new JComboBox(comboItems);
// empty combo box
model = new DefaultComboBoxModel(comboItems);
comboPlaylists.setModel(model);
Is there an easier way to do this? Or is my best option to have the playlists held in an array and search for a matching title each time? Any suggestions would be appreciated, thanks.