Changing scrollpane Viewport kills the mousewheel scroll

40 views Asked by At

all. I am in my first semester of Uni and am trying to create a mock-version of a streaming service. Currently the movies/series are shown in a grid that is created from an ArrayList of Media(a superclass). MenuItems have been added that can sort the Media. It does so by running creating a new sorted list and then making a new grid.

    private JScrollPane makeGrid(ArrayList<Media> gridList) throws IOException
{
    mediaPanel = new JPanel();
    mediaPanel.setLayout(new GridLayout(20,10));
    /*Path mediaPicPath = FileSystems.getDefault().getPath("MedieBilleder");
    File dir1;
    dir1 = mediaPicPath.toFile();*/
    File dir1 = new File("src/MedieBilleder");
    if(dir1.isDirectory()) {
        File[] mediaPicFiles;
        mediaPicFiles = dir1.listFiles();
        for(Media m : gridList) {
            for(File f : mediaPicFiles){
                String compare = f.toString().replace(".jpg","");
                if(!compare.contains("\\")){
                    String[] imgname = compare.split("/");
                    compare = imgname[2];
                }
                else{
                    String[] imgname = compare.split("\\\\");
                    compare = imgname[2];
                }
                compare.replaceAll("(^\\h*)|(\\h*$)","");
                if(m.getTitle().toLowerCase().trim().equals(compare.toLowerCase().trim())){
                    BufferedImage mediaPic = ImageIO.read(f);
                    JButton button = new JButton(new ImageIcon(mediaPic));
                    button.setBorder(BorderFactory.createEmptyBorder());
                    button.setContentAreaFilled(false);
                    button.addActionListener( e -> mediachoice(m.getTitle(),mediaPic));
                    mediaPanel.add(button);
                }
            }
        }
    }
    JScrollPane scroller = new JScrollPane(mediaPanel);
    scroller.getVerticalScrollBar().setUnitIncrement(16);
    return scroller;
}

The makeGrid method returns a JScrollPane which works with mousewhell. However, I have had issues on how to change the ScrollPane to the new Scrollpane whenever I want to sort my list.

For example by name from A to Z:

    private void aZ() {
    currentList = this.sorter.titleAZ(currentList);
    try{changeGrid(this.frame,currentscrollpane,makeGrid(currentList));}
    catch(IOException e){e.printStackTrace();}
} 

As you can see, this specific choice runs a changeGrid method:

    public void changeGrid(JFrame frame, JScrollPane currentscrollpane, JScrollPane newscrollpane)
{
    currentscrollpane.getViewport().remove(currentscrollpane);
    currentscrollpane.getViewport().add(newscrollpane);
    frame.repaint();

}

This method does change the shown list of movies but makes the scrollpane non-scrollable by mouse unless you scroll directly on the scrollbar. I imagine it's an issue with the way I replace the JScrollPane (but I couldn't figure out another way of doing it).

0

There are 0 answers