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

  • Home
  • SEARCH
  • 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 9153533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:17:15+00:00 2026-06-17T12:17:15+00:00

I have a JPanel with a vertical BoxLayout on top of a JPanel with

  • 0

I have a JPanel with a vertical BoxLayout on top of a JPanel with a null layout.

I would like the JPanel with the BoxLayout to grow as the components are being added.

See this code:

public static void main (String[] args) {
    JFrame f = new JFrame();
    f.setSize(500,500);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel total = new JPanel();
    total.setLayout(null);
    total.setSize(f.getWidth(),f.getHeight());
    total.setBackground(Color.green);
    JPanel box = new JPanel();
    box.setLocation(100,200);
    box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
    box.add(new JButton("test"));
    box.add(new JLabel("hey"));
    total.add(box);
    f.add(total);
    f.setVisible(true);
}

You will notice that no components show up.

How can I make the JPanel “box” such that the size dynamically increases as I add more components (which are added vertically).

IN ADVANCE: I need the position of “box” to be at exactly 100,200 so please do not suggest that I do not use null layout. I must use null layout. The null layout of “total” should not effect the answer to my question, which focuses on the “box” panel.

  • 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-17T12:17:15+00:00Added an answer on June 17, 2026 at 12:17 pm

    By throwing away the layout manager, you suddenly become responsible for it’s job. A job I might add, which isn’t easy …

    Basically, given you example, you’ve failed to set the size of the child component…

    JFrame f = new JFrame();
    f.setSize(500, 500);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JPanel total = new JPanel();
    total.setLayout(null);
    total.setSize(f.getWidth(), f.getHeight());
    total.setBackground(Color.green);
    
    JPanel box = new JPanel();
    box.setLocation(100, 200);
    box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
    box.add(new JButton("test"));
    box.add(new JLabel("hey"));
    box.setSize(100, 100);  // <-- Don't forget this..
    
    total.add(box);
    f.add(total);
    f.setVisible(true);
    

    Personally, I think your asking for trouble, but what would I know…

    A better idea might be to use something like an EmptyBorder to providing padding…

    enter image description here

    JFrame f = new JFrame();
    f.setSize(500, 500);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JPanel total = new JPanel(new BorderLayout());
    total.setSize(f.getWidth(), f.getHeight());
    total.setBackground(Color.green);
    total.setBorder(new EmptyBorder(100, 200, 100, 200));
    
    JPanel box = new JPanel();
    box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
    box.add(new JButton("test"));
    box.add(new JLabel("hey"));
    
    total.add(box);
    f.add(total);
    f.setVisible(true);
    

    Updated with Layout Manager example

    Now, if all the layout managers are failing you, you could try writing your own. This has the benefits you need from the null layout manager and the benefits of intergrating into Swing’s component changing process, without the need to resort to ComponentListeners and ContainerListeners

    enter image description here

    JPanel total = new JPanel();
    total.setLayout(new SuperAwesomeBetterThenYoursLayout());
    

    Custom Layout Manager

    public static class SuperAwesomeBetterThenYoursLayout implements LayoutManager {
    
        @Override
        public void addLayoutComponent(String name, Component comp) {
        }
    
        @Override
        public void removeLayoutComponent(Component comp) {
        }
    
        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return new Dimension(100, 300);
        }
    
        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return new Dimension(100, 300);
        }
    
        @Override
        public void layoutContainer(Container parent) {
            boolean laidOut = false;
            for (Component child : parent.getComponents()) {
                if (child.isVisible() && !laidOut) {
                    child.setLocation(200, 100);
                    child.setSize(child.getPreferredSize());
                } else {
                    child.setSize(0, 0);
                }
            }
        }
        
    }
    

    This basically represents the work you are going have to do anyway, but does it in a way that works with how Swing was designed…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an JPanel populated with several opaque custom components. Now I would like
I have a JPanel with null layout. On this panel I draw some custom
I have a JPanel, which I would like to detect the following events (1)
I have a JPanel with components added with absolute locations (ie. pos x y),
I have a JPanel that I want to use to contain 3 vertical components:
I have a JTextArea in a JPanel that I would like to use a
I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
I have a JPanel that encapsulates two JPanels, one on top of the other.
I have a JPanel with a Grid Layout. In the cells of the grid
I have a JPanel that implements custom drawing to draw a background. Over this,

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.