I am confused by the following.
Case A
Case B
Why does Case A throw the exception and Case B does not? Why do FlowLayout and BoxLayout behave differently in this regard? What does it mean in Case A by the "BoxLayout can't be shared"?
I have read a few similar threads on this website about this exception message but I'm still confused about the comparison to FlowLayout and what the exception message means exactly.
Class BoxLayout. A layout manager that allows multiple components to be laid out either vertically or horizontally. The components will not wrap so, for example, a vertical arrangement of components will stay vertically arranged when the frame is resized.
Box Layout Features. As said before, BoxLayout arranges components either on top of each other or in a row. As the box layout arranges components, it takes the components' alignments and minimum, preferred, and maximum sizes into account.
BorderLayout class implements a common layout style for top-level windows. Four fixed narrow, fixed-width components at edges and one large area in the center.
In the case of vertical orientations, the components are always rendered from top to bottom. BoxLayout (Container c, int axis): creates a box layout that arranges the components with the given axis. FileName: BoxLayoutExample1.java
The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It is the default layout of the applet or panel. Fields of FlowLayout class public static final int LEFT
Java BoxLayout. The BoxLayout is used to arrange the components either vertically or horizontally. For this purpose, BoxLayout provides four constants.
In the class FlowLayout we find the following constructors: creates a FlowLayout object with the default settings (centered Alignment of the lines, 5-pixel spacing). creates a FlowLayout object with an alignment according to align and the Standard setting for the distances.
Basically, "BoxLayout can't be shared"
in this situation means that you're trying to make your JFrame
and its contentPane()
share the same BoxLayout
object.
When setting the Layout
for a JFrame
, it implicitely calls getContentPane().setLayout(manager)
so you are actually setting the layout for the contentPane() and not for the frame itself.
This brings us to how the containers are checked within BoxLayout
and FlowLayout
.
FlowLayout
This layout does not have a constructor that will take the container as a parameter and thus will not, at the making of the object, consider the container. Neither has this class a container
instance variable.
BoxLayout
BoxLayout
on the contrary has a constructor that takes the container as a parameter and stocks it within the instance variable target
. This is done to check it later within the layoutContainer(container)
method. It has a checkContainer(container)
method that will verify if the instance variable is equals to the container given in parameter. It throws throw new AWTError("BoxLayout can't be shared");
if it is not the case.
This was an introduction to the following explaination.
As said within the first paragraph, JFrame.setLayout(LayoutManager)
will call JFrame.getContentPane().setLayout(LayoutManager)
and thus the Layout
is set on the contentPane()
which is a JPanel
by default.
Check out the constructor of BoxLayout(container, int)
and ask yourself :
Now I know that the Layout is set on the JPanel
(contentPane()
) and not the JFrame
itself, which parameter will I give to this constructor?
Is it a good idea to give it the JFrame
itself? Knowing that it is not the Component which on the Layout has been set?
The answer is : of course it is not a good idea.
Here is the correct way to achieve this :
JFrame frame = new JFrame();
frame.setLayout(new BoxLayout(frame.getContentPane(), ...);
Why?
Because we know now that the container is the contentPane()
and that eventually, when adding a component for example, a check is going to happen and the parameter within the constructor must be the exact same object
as the component where on the layout
is set which does not happens with FlowLayout
for example.
BoxLayout
BoxLayout(Container, int)
BoxLayout.checkContainer(Container)
BoxLayout.layoutContainer(Container)
FlowLayout
FlowLayout.layoutContainer(Container)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With