Search code examples
javaswingnulljpanelparent

My extended JPanel's parent is null


I have an extended JPanel class which creates a JPanel and adds it to its parent. getComponents called from the parent shows the panel correctly, but getParent called from the extended instance returns null.layeredPane

package testing;

import javax.swing.JButton;

public class ParentNullExample extends javax.swing.JFrame {

private static Panel1 mainPanel;
private static JButton AddButton;

private void createAndShowGUI() {
    //Set the look and feel.
    initLookAndFeel();

    setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    AddButton = new javax.swing.JButton();
    AddButton.setName("AddButton");
    AddButton.setText("Add a Row");
    AddButton.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addRowActionPerformed(evt);
        }
    });

    Panel1 componentPanel = new Panel1(true);
    componentPanel.setName("componentPanel");

    mainPanel = new testing.Panel1(false);
    mainPanel.setName("mainPanel");

    mainPanel.add(componentPanel);
    mainPanel.add(AddButton);

    mainPanel.setOpaque(true);
    setContentPane(mainPanel);

    pack();
    DebugUtils.analyseFrame(this);
}

private void addRowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addRowActionPerformed
    mainPanel.revalidate();
}//GEN-LAST:event_addRowActionPerformed

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new ParentNullExample().setVisible(true);
        }
    });
}
}

package testing;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import static javax.swing.BoxLayout.Y_AXIS;
import javax.swing.JPanel;

public class Panel1 extends JPanel {

    private JPanel mainPanel;
    private Dimension preferredSize;
    private GridLayout tableGridLayout = new GridLayout(0,1);
    private Boolean isGridLayout;

    public Panel1(Boolean gridLayout) {
        isGridLayout = gridLayout;
        mainPanel = new JPanel();
        if (gridLayout) {
            super.setLayout(tableGridLayout);
        }
        else {
            BoxLayout boxLayout = new BoxLayout(this,Y_AXIS);
            super.setLayout(boxLayout);
        }
        preferredSize = new Dimension();
    }

    @Override
    public void revalidate() {
        if (preferredSize == null) preferredSize = new Dimension();
        Component superParent = super.getParent();
        String superParentName = (superParent == null ? "null" : superParent.getName()) ;
        Component thisParent = this.getParent();
        String thisParentName = (thisParent == null ? "null" : thisParent.getName()) ;

        System.out.print("revalidate: "+
            "Name: "+this.getName()+", "+
            "super.Parent: "+superParentName+", "+
            "this.Parent: "+thisParentName+", "+
            "preferredSize: ["+preferredSize.getHeight() +", "+
            preferredSize.getWidth()+"]\n");

        if (superParent != null) superParent.revalidate();
        if (thisParent != null) thisParent.revalidate();
    };

    @Override
    public Dimension getPreferredSize() {
        return preferredSize;
    };

    @Override
    public Dimension getMaximumSize() {
        return new Dimension((int) super.getMaximumSize().getWidth(), (int) preferredSize.getHeight());
    };

    @Override
    public Component add(Component c) {
        super.add(c);
        preferredSize.setSize(addComponentSize(c, preferredSize));
        tableGridLayout.layoutContainer(mainPanel);
        return null;
    }

    public static Dimension addComponentSize(Component c, Dimension sizeSoFar) {
        int componentWidth = (int) c.getPreferredSize().getWidth();
        int panelWidth = (int) sizeSoFar.getWidth();
        int width = (panelWidth > componentWidth) ? panelWidth : componentWidth;
        int height = (int) (c.getPreferredSize().getHeight() + sizeSoFar.getHeight());
        return new Dimension(width, height);
    }

}

The output shows the parent name as "null.layeredPane" which is not the expected one.

revalidate: Name: null, super.Parent: null, this.Parent: null, preferredSize: [0.0, 0.0]
revalidate: Name: null, super.Parent: null, this.Parent: null, preferredSize: [0.0, 0.0]
revalidate: Name: null, super.Parent: null, this.Parent: null, preferredSize: [0.0, 0.0]
revalidate: Name: null, super.Parent: null, this.Parent: null, preferredSize: [0.0, 0.0]
1- Frame: Class: testing.ParentNullExample, Name: frame0, H: 61, W: 132
2|- Container: Name: mainPanel, Class: testing.Panel1, Layout: javax.swing.BoxLayout, H: 23, W: 116
3|-- Component: Class: testing.Panel1, Name: componentPanel, H: 0, W: 116
3|-- Component: Class: javax.swing.JButton, Name: AddButton, H: 23, W: 85


revalidate: Name: mainPanel, super.Parent: null.layeredPane, this.Parent: null.layeredPane, preferredSize: [23.0, 85.0]
revalidate: Name: mainPanel, super.Parent: null.layeredPane, this.Parent: null.layeredPane, preferredSize: [23.0, 85.0]

Solution

  • So, basically, what you seem to have is a misunderstand of the API. A JFrame is made up of a JRootPane, which contains a JLayeredPane, which contains the contentPane, JMenuBar and glassPane, as follows...

    RootPane

    So, when use setContentPane(mainPanel);, you're adding mainPanel to a JLayeredPane

    See How to use root panes for more details