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 5945241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:39:13+00:00 2026-05-22T16:39:13+00:00

The below code represents the problem. Since I have heights of the north and

  • 0

The below code represents the problem. Since I have heights of the north and south panels set the rest of it goes to the center panel using GridLayout. I think that since it cannot share the leftover pixels equally among its rows it just leaves them. Therefore in the below code we have ugly white line over south panel.

My question here is: How to make sure that when the GridLayout is not taking the whole space it is at least centered?

Normally I would use TableLayout and situation is sorted, but since I was writing an answer I wanted to use only standard managers. Knowing this would be very useful for me thanks in advance.

Example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AligningButonsTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {                   
                JFrame f = new JFrame();
                f.setSize(800, 600);
                double CONSTANT_FACTOR = .1;
                int noOfRows = 5;
                JPanel centerP = new JPanel(new GridLayout(noOfRows,1));

                for(int i = 0; i < noOfRows; i++)
                {   
                    BoxPanel bP = new BoxPanel();
                    centerP.add(bP);
                }
                JPanel contentPane = new JPanel(new BorderLayout());                
                f.setContentPane(contentPane);
                contentPane.add(centerP, BorderLayout.CENTER);
                JPanel southP = new JPanel();
                southP.setBackground(Color.RED.darker());//southP.setOpaque(false);
                southP.setPreferredSize(new Dimension(1, (int)(CONSTANT_FACTOR* f.getHeight())));
                contentPane.add(southP, BorderLayout.SOUTH);
                JPanel northP = new JPanel();
                northP.setBackground(Color.RED.darker());//northP.setOpaque(false);
                northP.setPreferredSize(new Dimension(1, (int)(CONSTANT_FACTOR* f.getHeight())));
                contentPane.add(northP, BorderLayout.NORTH);            
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }   
}

class BoxPanel extends JPanel
{
    public BoxPanel()
    {
        setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.RED));
        setBackground(Color.DARK_GRAY);
    }   
}
  • 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-05-22T16:39:14+00:00Added an answer on May 22, 2026 at 4:39 pm

    BoxLayout does a pretty good job of distributing the space between components using Box.createVerticalGlue(). This example uses Box.createVerticalStrut(), top and bottom. The spacers are described in How to Use BoxLayout: Using Invisible Components as Filler.

    Addendum: BoxTest2 is a variation that uses BoxLayout to create fixed-size edge panels and vertical glue to distribute the space more evenly. Box.Filler may also be used to control the “leftover” vertical space.

    /** @see http://stackoverflow.com/questions/6072956 */
    public class BoxTest2 {
    
        private static final int WIDE = 480;
        private static final int HIGH = WIDE / 8;
        private static final int ROWS = 5;
        private static final Box center = new Box(BoxLayout.Y_AXIS);
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    center.setOpaque(true);
                    center.setBackground(Color.lightGray);
                    center.add(Box.createVerticalGlue());
                    center.add(new EdgePanel());
                    for (int i = 0; i < ROWS; i++) {
                        center.add(new BoxPanel());
                    }
                    center.add(new EdgePanel());
                    center.add(Box.createVerticalGlue());
                    f.add(center, BorderLayout.CENTER);
                    f.pack();
                    f.setVisible(true);
                }
            });
        }
    
        private static class EdgePanel extends JPanel {
    
            public EdgePanel() {
                Dimension d = new Dimension(WIDE, 2 * HIGH / 3);
                setPreferredSize(d);
                setBackground(Color.red.darker());
            }
        }
    
        private static class BoxPanel extends JPanel {
    
            public BoxPanel() {
                setPreferredSize(new Dimension(WIDE, HIGH));
                setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.red));
                setBackground(Color.darkGray);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents,
The below code is very simple. I have a jQuery autocomplete bound to an
Given the Java code below, what's the closest you could represent these two static
Below code work only for latin n , but how to make it work
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
below code is my databasehandler class i got it from a tutorial. Beside that
Below code : URL oracle = new URL(http://www.oracle.com/); URLConnection inputStream =oracle.openConnection(); InputStream in =
Below code makes form to be submitted without html5 validation... $j(document).on(click, #client_entry_add, function(event){ ajax_submit();});
My below code works fine and is used to populate a <select> item with
The below code works when there are a few element in the To list

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.