Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a BoxLayout be shared whereas a FlowLayout can?

I am confused by the following.

Case A

  1. Set the layout manager for a JFrame as a BoxLayout.
  2. Add a JButton to the JFrame container.
  3. Compile.
  4. Run.
  5. Exception thrown: "Exception in thread "AWT-EventQueue-0" java.awt.AWTError: BoxLayout can't be shared"

Case B

  1. Set the layout manager for a JFrame as a FlowLayout.
  2. Add a JButton to the JFrame container.
  3. Compile
  4. Run
  5. No exception thrown.

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.

like image 897
danger mouse Avatar asked Nov 21 '15 10:11

danger mouse


People also ask

How does BoxLayout work?

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.

What is BoxLayout?

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.

Which layout is used to align fixed width components at the edges?

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.

What is the orientation of the components in boxlayout?

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

What is the use of FlowLayout class in 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

What is boxlayout in Java?

Java BoxLayout. The BoxLayout is used to arrange the components either vertically or horizontally. For this purpose, BoxLayout provides four constants.

What are the Constructors of the class FlowLayout?

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.


1 Answers

Simple answer

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.


Advanced Explanation

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.


Sources

BoxLayout

  • BoxLayout(Container, int)
  • BoxLayout.checkContainer(Container)
  • BoxLayout.layoutContainer(Container)

FlowLayout

  • FlowLayout.layoutContainer(Container)
like image 67
Yassin Hajaj Avatar answered Sep 23 '22 15:09

Yassin Hajaj