0

In short, I´m trying to make a program that mocks a streaming service interface for an assignment. The program creates two separate JScrollPanes with same specs (size, constraints..), one for movies and one for series, each containing a JPanel which then contains other movie or series components. Then, I created a JLayeredPane (without changing its layout), in which I want to add the two JScrollPanes as separate layers. However, when adding the JScrollPanes to the JLayeredPane and running the code, the JLayeredPane always shows the last added JScrollPane, regardless of the index I set upon adding.

        //creates the movie JScrollPane
        JScrollPane moviePanel = stuff.showMovies(movieLogic);

        //creates the series JScrollPane
        JScrollPane seriesPanel = stuff.showSeries(seriesLogic);

        //creates a JLayeredPane in which I want to insert the JScrollPanes as layers
        layerPanel = new JLayeredPane();
       

        //adds the moviePanel to index 3 and seriesPanel to index 5 in the JLayeredPane
        //I´m assuming index 3 is not as deep as index 5, meaning moviePanel should be in the front,
        //but I have also tried switching indexes just in case
        layerPanel.add(moviePanel, 3);
        layerPanel.add(seriesPanel, 5);
        
        //another attempt at getting the moviePanel to the front of the JLayeredPane, but
        //seriesPanel still shows up on top
        layerPanel.moveToFront(moviePanel); 

What am I missing or doing wrong?

Bubac
  • 13
  • 2
  • 1
    Best to create and post a decent [mre] program. Having said that, I do wonder if what you want to use instead is a CardLayout, a layout built to allow one to easily swap visualized components and containers. [CardLayout tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – DontKnowMuchBut Getting Better Dec 12 '20 at 16:31
  • 1
    This is what you're doing wrong: `layerPanel.setLayout(new BorderLayout());`. This nullifies the JLayeredPane's own internal layout and makes it non-functional. You should not be setting a JLayeredPane's layout explicitly and let it use its own native layout. You *can* set the layout of the containers that it holds, however. – DontKnowMuchBut Getting Better Dec 12 '20 at 16:33
  • 1
    Does this answer your question? [Java LayeredPane LayoutManager add() Method Conflict](https://stackoverflow.com/questions/37376915/java-layeredpane-layoutmanager-add-method-conflict) – DontKnowMuchBut Getting Better Dec 12 '20 at 16:35
  • Edited, I am no longer setting the layout of the layerPanel to a BorderLayout. Now the JLayeredPane shows nothing, I´m guessing because I have to manually set the size and position of the JScrollPanes in the layers, but I can´t figure out how to set it such that it just takes up all the available space in the layer. Can you point me in the right direction? Will also proceed with googling of course. – Bubac Dec 12 '20 at 17:03
  • Just use a CardLayout as already suggested – Hovercraft Full Of Eels Dec 12 '20 at 17:14
  • To set a component's size to its container's size, get the container's size (or preferred size, depending on when you get it) and set the component's size: `myComponent.setSize(itsContainer.getPreferredSize());` But yeah, use a CardLayout if you're trying to swap components. – DontKnowMuchBut Getting Better Dec 12 '20 at 17:19
  • Actually, thinking about this further, the best solution is to use a single JScrollPane that holds a JList and simply change the JList's model. – DontKnowMuchBut Getting Better Dec 12 '20 at 17:30
  • I went with a simple JPanel with a CardLayout to which I then add the JScrollPanes, as suggested by both. Works like a charm. Thanks! – Bubac Dec 12 '20 at 17:33

1 Answers1

1

Again, the simplest solution might be to use a JList and swap models. For example the following uses two models, one that displays color Strings the other days of the week, and the models are swapped in a button's action listener:

import java.awt.BorderLayout;    
import javax.swing.*;

public class SwapStuff extends JPanel {
    public static final String[] DAYS_OF_WEEK = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
            "Saturday" };
    public static final String[] COLORS = { "Red", "Orange", "Yellow", "Green", "Blue", "Violet" };
    private DefaultListModel<String> dayModel = new DefaultListModel<>();
    private DefaultListModel<String> colorModel = new DefaultListModel<>();
    private JList<String> list = new JList<>(dayModel);
    private JTextField displayField = new JTextField(20);

    public SwapStuff() {
        for (String element : DAYS_OF_WEEK) {
            dayModel.addElement(element);
        }
        for (String element : COLORS) {
            colorModel.addElement(element);
        }
        list.setVisibleRowCount(5);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(list);

        displayField.setFocusable(false);

        JButton weekdayBtn = new JButton("Weekdays");
        JButton colorBtn = new JButton("Colors");

        weekdayBtn.addActionListener(e -> {
            if (dayModel != list.getModel()) {
                list.setModel(dayModel);
            }
        });
        colorBtn.addActionListener(e -> {
            if (colorModel != list.getModel()) {
                list.setModel(colorModel);
            }
        });
        list.addListSelectionListener(e -> {
            String selection = list.getSelectedValue();
            displayField.setText(selection);
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(weekdayBtn);
        buttonPanel.add(colorBtn);

        setLayout(new BorderLayout());
        add(displayField, BorderLayout.PAGE_START);
        add(scrollPane);
        add(buttonPanel, BorderLayout.PAGE_END);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        SwapStuff mainPanel = new SwapStuff();
        JFrame frame = new JFrame("SwapStuff");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}