0

I have some issues making a JScrollPane scrolling in Swing. I don't know why it is doing that. It worked before when I just created a pane on top of the contentPane. But it is not working anymore now that I added 2 other pane to the contentPane. (There is a total of 25 videos fetched here so I should have a scroll bar on the right side of the leftPannel).

package projet.interfaces;

import projet.listeners.BouttonAddListener;
import projet.model.Musique;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.List;

public class Window extends JFrame {

    private final JTextArea lien = new JTextArea();
    private final JButton linkButton = new JButton();
    private JPanel topPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JScrollPane leftScrollPane;
    private int nombreVideos = 0;

    public Window() {
        super("Youtube and Soundcloud playlist downloader");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(1920,1080);
        this.setLocationRelativeTo(null);
    }

    public void initializeWindow() {
        topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(this.getWidth(), 100));
        addLien();
        addButtonLien();

        leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
        leftPanel.setMaximumSize(new Dimension(leftPanel.getWidth(),Integer.MAX_VALUE));
        leftScrollPane = new JScrollPane(leftPanel);
        leftScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        rightPanel = new JPanel();

        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());

        contentPane.add(topPanel, BorderLayout.NORTH);
        contentPane.add(leftPanel, BorderLayout.WEST);
        contentPane.add(rightPanel, BorderLayout.CENTER);
    }

    private void addLien() {
        lien.setBounds(25,15,200,20);
        lien.setText("Copiez votre URL ici.");
        topPanel.add(lien);
    }

    private void addButtonLien() {
        linkButton.setBounds(245,15,20,20);
        linkButton.setText("V");
        linkButton.addActionListener(new BouttonAddListener(this));
        topPanel.add(linkButton);
    }


    public void addVideos(List<Musique> musiques) {
        for (Musique musique:musiques) {
            JPanel box = createBox(musique.getTitre(), musique.getArtiste().getNom(), musique.getImage());
            leftPanel.add(box);
            nombreVideos++;
        }
        leftPanel.revalidate();
        leftPanel.repaint();
    }

    private JPanel createBox(String title, String artist, String imagePath) {
        JPanel box = new JPanel();
        box.setLayout(new BorderLayout(10, 10));
        box.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        JLabel titleLabel = new JLabel("Title: " + title);
        JLabel artistLabel = new JLabel("Artist: " + artist);

        // Load and display the image
        BufferedImage image = loadImage(imagePath);
        if (image != null) {
            JLabel imageLabel = new JLabel(new ImageIcon(image));
            box.add(imageLabel, BorderLayout.CENTER);
        }

        box.add(titleLabel, BorderLayout.NORTH);
        box.add(artistLabel, BorderLayout.SOUTH);

        return box;
    }

    private BufferedImage loadImage(String imagePath) {
        try {
            return ImageIO.read(new URL(imagePath));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public JTextArea getLien() {
        return lien;
    }
}

the result is here Thanks for your help :). Have a good day.

I tried /

  • leftPanel.setLayout(new GridLayout(0,1)); (it is the one solution that worked before i added 2 other panes)
  • Just having a JscrollPane and adding elements into it but it is not showing anything :/
1
  • I don't know if I missed it, but I don't see the code that calls addVideos. Also, not really a good idea to call your class 'Window' (the same as one in your imports)
    – g00se
    Commented Oct 3, 2023 at 13:07

1 Answer 1

2

Okay I found out where it came :

contentPane.add(leftPanel, BorderLayout.WEST);

You have to put the leftScrollPanel instead of the leftPanel in the contentPane :

contentPane.add(leftScrollPane, BorderLayout.WEST);

Not the answer you're looking for? Browse other questions tagged or ask your own question.