Reputation: 69
I wrote up this quick example to see what happens when the value of an area in a BorderLayout
is superimposed with new content (or replaced?).
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JPanel add1 = new JPanel();
add1.setBackground(Color.RED);
JPanel add2 = new JPanel();
add2.setBackground(Color.DARK_GRAY);
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(add1, BorderLayout.NORTH);
pane.add(add2, BorderLayout.NORTH);
JFrame main = new JFrame();
main.setContentPane(pane);
main.setPreferredSize(new Dimension(300,300));
main.setLocation(200,300);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setResizable(false);
main.setVisible(true);
main.pack();
}
}
What I was wondering is what happens to add1
when BorderLayout.NORTH
is reassigned? Does the garbage collector handle it?
I would assume not as there is still a reference to add1
inside the class Test
, however I'm not sure if it is still considered part of the JFrame. If I'm going to be replacing BorderLayout.NORTH
, will I need to set add1
to null
? I guess what I'm asking is if a layout reference is replaced, is the new value superimposed into that position, or is the old value replaced completely and thus no longer part of the JFrame.
Upvotes: 0
Views: 33
Reputation: 285405
As per most similar questions, the Java API holds the answers. Here the BorderLayout API specifically states (highlighting mine):
A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER.
So by adding a new component to the NORTH section, you displace the old one, and it is no longer part of the GUI.
Upvotes: 4