Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8021167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:54:01+00:00 2026-06-04T21:54:01+00:00

Most layout managers have no-argument constructors (that is, you can create a FlowLayout with

  • 0

Most layout managers have no-argument constructors (that is, you can create a FlowLayout with new FlowLayout (), a GridLayout with new GridLayout (), a GridBagLayout with new GridBagLayout (), etc.). However, BoxLayout requires that you pass both the container that it will be managing and the axis along which the components should be laid out.

My question is: since you’re already telling the layout manager which component to lay out, why do you need to write

BoxLayout bl = new BoxLayout(myPanel, BoxLayout.Y_AXIS);
myPanel.setLayout(bl);

instead of just the first line?

I took a quick look at the BoxLayout source code and saw that the constructor I use (lines 178-185) doesn’t make a call to target.setLayout(this) or anything of the sort. It seems like it would be really simple to just add that. Is there a reason why it’s not included in the Swing library?

If it matters, I’m using

java version 1.7.0

Java(TM) SE Runtime Environment (build 1.7.0-b147)

on Win7Pro.

Thanks!


SSCCE:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class BoxLayoutSSCCE extends JFrame {

    // Change this to see what I mean
    public static final boolean CALL_SET_LAYOUT = true;

    public BoxLayoutSSCCE () {
        super("Box Layout SSCCE");
        JPanel panel = new JPanel();
        BoxLayout bl = new BoxLayout(panel, BoxLayout.Y_AXIS);
        if (CALL_SET_LAYOUT) {
            panel.setLayout(bl);
        }
        panel.add(new JButton("Button 1"));
        panel.add(new JButton("Button 2"));
    }

    public static void main (String[] args) {
        BoxLayoutSSCCE blsscce = new BoxLayoutSSCCE();
        blsscce.pack();
        blsscce.setVisible(true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:54:04+00:00Added an answer on June 4, 2026 at 9:54 pm

    The Container must exist before it can be passed to BoxLayout. Typically one writes something like this:

    JPanel myPanel = new JPanel();
    BoxLayout bl = new BoxLayout(myPanel, BoxLayout.Y_AXIS);
    myPanel.setLayout(bl);
    

    It’s tempting to combine the last two lines, but the principle of least astonishment suggests that the layout’s constructor should not otherwise alter the container’s state.

    Convenienly, javax.swing.Box provides “A lightweight container that uses a BoxLayout object as its layout manager.”

    public class Box extends JComponent implements ... {
    
        public Box(int axis) {
            super();
            super.setLayout(new BoxLayout(this, axis));
        }
    }
    

    Now a single line will do:

    Box myBox = new Box(BoxLayout.Y_AXIS);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I have a layout where there is a view occupying most of
Most apps have some kind of preferences or user settings that can be stored
I have a sidebar defined in my main layout which most of the time
I have a ListView in which each item has a complex layout that contains,
I have a custom search panel, which is a part of main layout. Most
I am new in css. My project is Layout Manager . Most of the
Most subversion tools create a default repository layout with /trunk, /branches and /tags. The
I have several view components in a layout. most of them have visibility=gone and
Most popular web-sites that require you to log in, have the authentication form on
Most of the print pdf library I ran into requires drawing tables, layouts etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.