1

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
    }
}
gprathour
  • 14,813
  • 5
  • 66
  • 90
johnguild
  • 435
  • 1
  • 5
  • 25
  • Try to use another layout manager. Like FlowLayout. – Ivan Aug 08 '14 at 01:09
  • 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 – MadProgrammer Aug 08 '14 at 01:38

1 Answers1

2

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

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • omg i feel so dumb... and also thanks MadProgrammer for the additional info about layouts(that's where my problems always occur). cheers ^_^ – johnguild Aug 08 '14 at 11:02
  • Welcome to my world :P – MadProgrammer Aug 08 '14 at 11:12
  • lol, i think i'll take this chance to ask you for a link that will give an explanation of the ContentPane and how important it is in application interface.. tnx in advance. :) – johnguild Aug 08 '14 at 11:16
  • The short answer is, very, have a look at [How to Use Root Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) which should give a bit more of an idea of how it all hangs together – MadProgrammer Aug 08 '14 at 12:01