LayoutManager for JFrame contentPane

As mentioned here: Adding components to the content pane ,

The default content pane is a simple intermediate container that inherits from JComponent and uses BorderLayout as a layout manager.

and here is the proof:

JFrame frame = new JFrame(); LayoutManager m = frame.getContentPane().getLayout(); System.out.println(m instanceof BorderLayout); // prints true 

However, can you explain the output of the following code?

 JFrame frame = new JFrame(); LayoutManager m = frame.getContentPane().getLayout(); System.out.println(m); System.out.println(m.getClass().getName()); LayoutManager m2 = new BorderLayout(); System.out.println(m2); System.out.println(m2.getClass().getName()); 

CONCLUSION:

 javax.swing.JRootPane$1[hgap=0,vgap=0] javax.swing.JRootPane$1 java.awt.BorderLayout[hgap=0,vgap=0] java.awt.BorderLayout 
+4
source share
1 answer

this explains your result:

  protected Container createContentPane() { JComponent c = new JPanel(); c.setName(this.getName()+".contentPane"); c.setLayout(new BorderLayout() { /* This BorderLayout subclass maps a null constraint to CENTER. * Although the reference BorderLayout also does this, some VMs * throw an IllegalArgumentException. */ public void addLayoutComponent(Component comp, Object constraints) { if (constraints == null) { constraints = BorderLayout.CENTER; } super.addLayoutComponent(comp, constraints); } }); return c; } 

The contentpane creation method creates an anonymous inner class that inherits BorderLayout. So testing for instanceof will return true, but its a different class, so the class name is different.

+5
source

All Articles