Search code examples
javaswingjpanelboxlayout

I want to use a BoxLayout in a ScrollPane that has an ArrayList of Panels


When I set the outpanel into a BoxLayout then the panel disappears. However the scrollbar shows that indicates my panel in ArrayList are in the right position. I am totally new to Java so I'll appreciate any comments.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.*;
import javax.swing.*;

public class gui extends JFrame{

    int ctr=0, top=5;
    public List<JPanel> o_panels = new ArrayList<JPanel>(); //Your List    

    public gui(){
        super("MCC");
        setLayout(null);
        //Output panel for the results
        JPanel outpanel = new JPanel();
        outpanel.setBackground(Color.blue); 
        outpanel.setVisible(true);
        outpanel.setLayout(new BoxLayout(outpanel, BoxLayout.PAGE_AXIS));           
        //Scroll pane
        JScrollPane scrollPane = new JScrollPane(outpanel,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);            
        scrollPane.setBounds(0,0,780,400);
        add(scrollPane);            
        //result panel
        //creating and adding panels in to the array list
        while(ctr<=4){
            JPanel label1 = new JPanel();
            label1.setPreferredSize(new Dimension(600,100));
            o_panels.add(label1);
            outpanel.add(o_panels.get(ctr));        
        ctr++;
        }
    }

    public void runGui(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setVisible(true);
        this.setResizable(false);
        //i call this on the other class
    }
}

Solution

  • There is, not much, wrong with your code, the problem is, you've not established any means by which you can see what you've been adding

    Have a look at this...

    while (ctr <= 4) {
        JPanel label1 = new JPanel();
        label1.setPreferredSize(new Dimension(600, 100));
        o_panels.add(label1);
        outpanel.add(o_panels.get(ctr));
        ctr++;
    }
    

    All the panels are the same color and you've added nothing to them, so how could you possible know if they were been added or layout correctly...

    I simple added label1.setBorder(new LineBorder(Color.RED)); and got this result...

    Panels

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.border.LineBorder;
    
    public class Test extends JFrame {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
                    Test test = new Test();
                    test.runGui();
                }
            });
        }
    
        int ctr = 0, top = 5;
    
        public List<JPanel> o_panels = new ArrayList<JPanel>(); //Your List
    
        public Test() {
            super("MCC");
            //Output panel for the results
            JPanel outpanel = new JPanel();
            outpanel.setBackground(Color.blue);
            outpanel.setVisible(true);
            outpanel.setLayout(new BoxLayout(outpanel, BoxLayout.PAGE_AXIS));
    
            //Scroll pane
            JScrollPane scrollPane = new JScrollPane(outpanel,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
            scrollPane.setBounds(0, 0, 780, 400);
            add(scrollPane);
    
        //result panel
            //creating and adding panels in to the array list
            while (ctr <= 4) {
                JPanel label1 = new JPanel();
                label1.setPreferredSize(new Dimension(600, 100));
                label1.setBorder(new LineBorder(Color.RED));
                o_panels.add(label1);
                outpanel.add(o_panels.get(ctr));
                ctr++;
            }
        }
    
        public void runGui() {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            this.setVisible(true);
    //        this.setResizable(false);
            setLocationRelativeTo(null);
        }
    }
    

    Also have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

    And you really should avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify