Search code examples
javaswinglayout-managerboxlayout

Java Horizontal BoxLayout with Vertical Layout Inside


Okay, so I have a BoxLayout set to Y_AXIS for a main panel I have in a JFrame. I tried to set the actual Frame to a BoxLayout set to X_AXIS then add my main panel to my JFrame, but I get an java.awt.AWTError saying: "BoxLayout can't be shared". Is there any other way of using BoxLayout that will allow me to do this?

ComponentPanel.setToolTipText("I go in the FramePanel as well as other of my types aligned up and down");

FramePanel.setLayout(new BoxLayout(FramePanel, BoxLayout.Y_AXIS));
FramePanel.add(ComponentPanel);

Frame.setLayout(new BoxLayout(Frame, BoxLayout.X_AXIS));
Frame.add(FramePanel);

Solution

  • Use the JFrame's content panel as the BoxLayout's target container:

    frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
    

    Also, please try to follow Java Naming Conventions. Variable names should start with a lowercase letter; class names should start with an uppercase letter.