1

I want to know can we have a JPanel with a Layout other than its Parent JFrame. For example. If I have JFrame with Border Layout and we have a JPanel embedded on it and it is having different Layout. Is it possible ?

I am trying to do it. But this way the components of that JPanel are not showing.

Here comes the problem in detail :

I have a JFrame and layout for it is Border Layout. I am adding a JPanel on this frame. If I donot set any Layout for the JPanel. All components of JPanel are displaying on the window but When I am setting Grid Layout for JPanel, Components of JPanel are not visible. I am adding Layout to JPanel so as to align the Components. Below is my code :

I have a main class, a frame class and Jpanel class.

    public class AppMain {

    public static void main(String[] args) {

        AppPage1 page1 = new AppPage1("test");
        page1.setVisible(true);
    }
}



public class AppPage1 extends JFrame {

    public AppPage1(String title) throws HeadlessException {

        super(title);
        this.setLayout(new BorderLayout());

        addWindowListener(new WindowAdapter() {

            public void windowOpened(WindowEvent e) {
                setExtendedState(MAXIMIZED_BOTH);
            }
        });

        //Panel for logo
        JLabel testLogo = new JLabel("");
        testLogo.setIcon(new javax.swing.ImageIcon("test.JPG"));
        List<JComponent> componentList = new ArrayList<JComponent>();
        componentList.add(testLogo);


        PagePanel logoPanel = new PagePanel(componentList,null);
        this.add(logoPanel, BorderLayout.NORTH);


        //Panel for Button and checkboxes
        JLabel panelTitle = new JLabel("test Wizard");
        JRadioButton rdButton_ExistingConfigurationFile = new JRadioButton("Existing Configuration File");
        JRadioButton rdButton_ConfigureNewPropertyFile = new JRadioButton("Configure new Property File");

        componentList = new ArrayList<JComponent>();
        componentList.add(panelTitle);
        componentList.add(rdButton_ExistingConfigurationFile);
        componentList.add(rdButton_ConfigureNewPropertyFile);

        PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this));
        this.add(buttonPanel, BorderLayout.CENTER);

        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        validate();
    }
}



    public class PagePanel extends JPanel {

    public PagePanel(List<JComponent> componentList, LayoutManager layOutManager) {

        this.setBackground(Color.decode("#4E6987"));

        if (layOutManager != null) {
            this.setLayout(null);
        }
        for (JComponent jComponent : componentList) {

            jComponent.setBackground(Color.decode("#4E6987"));
            this.add(jComponent);
        }
    }
}

Thanks in Advance Ravi Kumar

Ravi.Kumar
  • 761
  • 2
  • 12
  • 34
  • Yes it is possible. Though why it isn't happening on your side, for this you need to show exactly what you doing !! That will help anyone answering you, so some code from your side will be much appreciated. By default `JFrame has BorderLayout` and `JPanel has FLowLayout`, so instead of doing anything simply add components on your `JPanel` and add this to your `JFrame` without any other line, you will get "YES" as an answer to your question :-) – nIcE cOw Jun 19 '12 at 05:35
  • Of course you can have a different layout manager for each Component of the hierarchy. By default JPanel uses a FlowLayout. If you encounter troubles, post an [SSCCE](http://sscce.org) for better help. – Guillaume Polet Jun 19 '12 at 05:35

2 Answers2

0

I could write an example demonstrating this is perfectly possible, but SO already has a great 'Nested layout' example, so a link will suffice

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
0

The problem is that you set a null layout manager instead of setting a layout manager in your PagePanel class.

if (layOutManager != null) {
    this.setLayout(null);
}

If you set a null layout, you have to position and size the components "manually", so try to avoid that as much as possible and use properly LayoutManager's.

This line does not make sense either:

PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this));

The argument of the GroupLayout constructor should be the container on which you set the layout, not a random component.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117