It's a lot of code, and not really cleanly structured and so. But as far as I can see, the problem can not solved by only "repaining" anything. The problem is that the labels (showing the media images) are not added to the shelf.
You are currently calling the buildShelfs
method once, when the frame is constructed. When you are later adding a new movie via the MediaHandler
, you are adding this movie to the mediaList
. But you are not adding a new label/image to the mediaPanel
.
This can probably be solved as follows: You can declare the mediaPanel
as an instance variable. And you can offer a method to add a new ImageIcon
to this panel, with an image that was read from a particular URL (given as a String). This method can on the one hand be used during the initialization (that is, during the first call to buildShelfs
). But more importantly: You can also call this method from the MediaHandler
, so that a new ImageIcon is added to the mediaPanel
whenever a new movie is added in the MovieHandler
.
I tried to sketch the basic idea in the following code. But again, the structure of the current code is not really nice and clean, so there was some guesswork involved....
class Window ...
{
// Store this as an instance variable
private JPanel mediaPanel;
private JPanel buildShelfs()
{
mediaPanel = new JPanel();
mediaPanel.setOpaque(false);
mediaPanel.setLayout(new GridLayout(3, 6, 22, 30));
mediaPanel.setBounds(90, 25, 1020, 745);
for (int i=0; i<18; i++)
{
addMedia(i);
}
return mediaPanel;
}
private void addMedia(int index)
{
// You should store these images LOCALLY!!!
String urlString = "https://dl.dropboxusercontent.com/u/16670644/Projekt/TempPic.png";
int nrOfMedias = myMediaHandler.mediaList.size();
if (index < nrOfMedias)
{
urlString = myMediaHandler.mediaList.get(i).getImagePath();
}
addMedia(urlString);
}
void addMedia(String urlString)
{
try
{
java.net.URL where = new URL(urlString);
ImageIcon image = new ImageIcon(where);
JLabel imageLabel = new JLabel(image);
mediaPanel.add(imageLabel);
mediaPanel.invalidate();
mediaPanel.validate();
}
catch (MalformedURLException e)
{
System.out.println("Error loading user media picture from "+urlString);
e.printStackTrace();
}
}
}
class MediaHandler
{
ArrayList<Media> mediaList;
private Window myWindow;
public MediaHandler(Window window)
{
this.mediaList = new ArrayList<Media>();
myWindow = window;
}
public void addMovie(String title, Double playTime, int year,
String directory, String mediaPath, String imagePath, String quality,
boolean subtitles, String language, String writer)
{
mediaList.add(new Movie(title, playTime, year,
false, directory, mediaPath, imagePath, rating.unrated,
quality, subtitles, language, writer));
myWindow.addMedia(mediaPath);
}
}