0

I wanted to create card view using jpanel array with the below code but it is not working the code is as:

This is to be same as that a facebook post on web I wanted to create it on gui

You may also suggest me other techniques to perform the same operation it would be a great help.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;

public class test extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test frame = new test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

public test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 587, 391);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    JPanel[] pan = new JPanel[5];
    int j= 0;
    for(int i= 0;i<5;i++)
    {
        pan[i].setBounds(28, 22+j, 522, 149);
        contentPane.add(pan[i]);
        pan[i].setVisible(true);    
        j= j+160;

    }


}
}

error

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • 1
    `contentPane.setLayout(null);` and `pan[i].setBounds(28, 22+j, 522, 149);` will break your GUI sooner or later, Swing was designed to use [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html), pixel perfect GUIs are an illusion, Swing has to work with different PLAFs, OS, screen sizes and resolutions, you'll face a lot of problems trying to solve problems related to the use of null layout in the future and will give you a lot of trouble and headaches trying to solve them... – Frakcool Jan 26 '17 at 16:45
  • 2
    ... See [Null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout in Swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing). You also shouldn't be extending from `JFrame`, see [this question](http://stackoverflow.com/questions/41252329/java-swing-using-extends-jframe-vs-calling-it-inside-of-class) and the answers in it too – Frakcool Jan 26 '17 at 16:47
  • 1
    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 Jan 26 '17 at 19:45

1 Answers1

1

You need to initialize your panels

for(int i= 0;i<5;i++)
{
    pan[i] = new JPanel(); // this line missing
    pan[i].setBounds(28, 22+j, 522, 149);
    contentPane.add(pan[i]);
    pan[i].setVisible(true); // not required
    pan[i].setBorder(new LineBorder(Color.BLACK)); // to see bounds of your panels
    j= j+160;
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48