user2117945
user2117945

Reputation: 35

Dynamic adding JPanel to another JPanel in Java Swing

I am very new for the java swing and so does java. I use netbeans' window's builder to design the GUI. I have a JPanel called orderListPanel which includes a JPanel called orderListRowPanel. Now, I got a list of JPanel and I want to insert these JPanels to the orderListPanel.

http://postimage.org/image/ex00whf69/
orderListPanel is in the middle and orderListRowPanel is just the same place like orderListPanel

http://postimage.org/image/dbrtn33sj/
right now I wanna insert many JPanels into orderListPanel and make it look like a list. The red squares are the components in side the JPanel.

I try to use BorderLayout and when I use foreach loop orderListPanel.add(List pList), I can't see any result inside the orderListPanel. Do anybody know how to solve it?

Upvotes: 1

Views: 7737

Answers (1)

PeakGen
PeakGen

Reputation: 22995

Do not use Netbeans automated GUI builder. Drag and Drop GUI building technique is not acceptable in Java community. You have not provided the code so we can't do a code edit. Your images are not available.

But however, this is how you can do it. This is a pure hand code

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.List;

public class GUIBuilder extends JFrame
{
    private JPanel orderList;
    private JPanel orderListRow;
    private JPanel additionalPanel;

    private List panels = new ArrayList(); //Your List

    private JLabel label1, label2, label3;

    public GUIBuilder()
    {
        label1 = new JLabel("Label 1"); //Create the JLabels
        label2 = new JLabel("Label 2");//Create the JLabels
        label3 = new JLabel("Label 3");//Create the JLabels


        orderList = new JPanel(); //Creating the orderList JPanel
       orderList.setLayout(new BoxLayout(orderList, BoxLayout.Y_AXIS)); //Setting Box layout, and set the direction to Y axis.


        orderListRow = new JPanel(); //Creating the orderListRow JPanel        
        orderListRow.add(label1);

        additionalPanel = new JPanel(); //Creating the additionalPanel JPanel      
        additionalPanel.add(label2);

        orderList.add(orderListRow); //Adding orderListRow into orderList
        orderList.add(additionalPanel); //Adding additionalPanel into orderList

        this.setLayout(new GridLayout(1,1));
        this.add(orderList); //Setting orderList into JFrame

        this.pack(); //Setting JFrame size. This will only take required space
        this.setVisible(true); //Making JFrame Visible
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //When you hit the 'X' button, the program will exit
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //Setting the UI into your native platform UI
            new GUIBuilder(); //Calling your program
        }
        catch(Exception e)
        {
            e.printStackTrace(); //If any error occured in setting up UI, print the stack trace
        }
    }
}

If you got the Panels inside a List, then use this

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.List;

public class GUIBuilder extends JFrame
{
    private JPanel orderList;
    private JPanel orderListRow;
    private JPanel additionalPanel;

    private List<JPanel> panels = new ArrayList<JPanel>(); //Your List

    private JLabel label1, label2, label3;

    public GUIBuilder()
    {
        label1 = new JLabel("Label 1"); //Create the JLabels
        label2 = new JLabel("Label 2");//Create the JLabels
        label3 = new JLabel("Label 3");//Create the JLabels


        orderList = new JPanel(); //Creating the orderList JPanel
        orderList.setLayout(new BoxLayout(orderList, BoxLayout.Y_AXIS)); //Setting Box layout, and set the direction to Y axis.


        orderListRow = new JPanel(); //Creating the orderListRow JPanel        
        orderListRow.add(label1);
        panels.add(orderListRow); // Add the panel to the List

        additionalPanel = new JPanel(); //Creating the additionalPanel JPanel      
        additionalPanel.add(label2);
        panels.add(additionalPanel); // Add the panel to the List


        for(int i=0;i<panels.size();i++)
        {
            orderList.add(panels.get(i));
        }



        this.setLayout(new GridLayout(1,1));
        this.add(orderList); //Setting orderList into JFrame

        this.pack(); //Setting JFrame size. This will only take required space
        this.setVisible(true); //Making JFrame Visible
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //When you hit the 'X' button, the program will exit
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //Setting the UI into your native platform UI
            new GUIBuilder(); //Calling your program
        }
        catch(Exception e)
        {
            e.printStackTrace(); //If any error occured in setting up UI, print the stack trace
        }
    }
}

Upvotes: 2

Related Questions