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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:48:29+00:00 2026-05-30T17:48:29+00:00

I have a medium-large UI that uses a BorderLayout; the center is a tabbed

  • 0

I have a medium-large UI that uses a BorderLayout; the center is a tabbed pane containing various panels with various layouts, etc.

I want the panel in the center of this border layout to resize according to the size of
the window, but I don’t want the components within the panel to stretch. Labels, combo boxes, text fields, buttons — I want them to stay at their preferred sizes, and allow the panel that contains them to stretch. I put them in a scroll pane in case the space gets too small for the panel.

Various posters with colorful vocabularies warn against the danger of using any of the setXXXsize() methods on components. That’s what I do now, and I’d like to learn how to avoid it.

GridBagLayout is not appropriate for some of my panels. It is, by nature, oriented around rows and columns, and not everything fits into rows and columns. Of course I could create artificial rows and columns to fit everything into, but I’m really hoping Swing has more layout options than that.

Vertical Glue doesn’t do it either. I’ve included it in HFOE’s beloved SSCE:

    package example;

    import java.awt.BorderLayout;

    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class BorderAndBox extends JFrame
    {
        public static void main(String args[])
        {
            BorderAndBox bnb = new BorderAndBox();
            bnb.createUI();
            bnb.setVisible(true);
        }

        public void createUI()
        {
            JPanel borderPanel = new JPanel(new BorderLayout());

            JLabel northLabel = new JLabel("Nawth");
            borderPanel.add(northLabel, BorderLayout.NORTH);

            String[] southComboChoices = { "one", "two", "three" };
            JComboBox southCombo = new JComboBox(southComboChoices);
            borderPanel.add(southCombo, BorderLayout.SOUTH);

            JPanel centerPanel = new JPanel();
            centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
            String[] firstChoices = { "first", "uno", "UN" };
            String[] secondChoices = { "second", "dos", "zwei" };
            String[] thirdChoices = { "third", "tres", "drei" };
            JComboBox firstCombo = new JComboBox(firstChoices);
            JComboBox secondCombo = new JComboBox(secondChoices);
            JComboBox thirdCombo = new JComboBox(thirdChoices);
            centerPanel.add(firstCombo);
            centerPanel.add(secondCombo);
            centerPanel.add(thirdCombo);
            centerPanel.add(Box.createVerticalGlue());  // first attempt; does NOT
            // take up available vertical space, instead it appears to create a space
            // that is shared equally among the (now) four components of this space.
            borderPanel.add(centerPanel, BorderLayout.CENTER);

            getContentPane().add(borderPanel);
            pack();
        }

    }

If you enlarge the window, the comboboxes in the center enlarge; as written, a vertical glue piece below them also enlarges, but doesn’t take up all available space. It appears it is given as much space as each of them.

So what is a good way to approach this?

  • 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-30T17:48:30+00:00Added an answer on May 30, 2026 at 5:48 pm

    BorderAndBox - centered

    import java.awt.BorderLayout;
    import java.awt.GridBagLayout;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class BorderAndBox extends JFrame
    {
    public static void main(String args[])
    {
        BorderAndBox bnb = new BorderAndBox();
        bnb.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        bnb.createUI();
        bnb.setVisible(true);
    }
    
    public void createUI()
    {
        JPanel borderPanel = new JPanel(new BorderLayout());
    
        JLabel northLabel = new JLabel("Nawth");
        borderPanel.add(northLabel, BorderLayout.NORTH);
    
        String[] southComboChoices = { "one", "two", "three" };
        JComboBox southCombo = new JComboBox(southComboChoices);
        borderPanel.add(southCombo, BorderLayout.SOUTH);
    
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
        String[] firstChoices = { "first", "uno", "UN" };
        String[] secondChoices = { "second", "dos", "zwei" };
        String[] thirdChoices = { "third", "tres", "drei" };
        JComboBox firstCombo = new JComboBox(firstChoices);
        JComboBox secondCombo = new JComboBox(secondChoices);
        JComboBox thirdCombo = new JComboBox(thirdChoices);
        centerPanel.add(firstCombo);
        centerPanel.add(secondCombo);
        centerPanel.add(thirdCombo);
        centerPanel.add(Box.createVerticalGlue());  // first attempt; does NOT
        // take up available vertical space, instead it appears to create a space
        // that is shared equally among the (now) four components of this space.
        JPanel centerPanelConstrain = new JPanel(new GridBagLayout());
        centerPanelConstrain.add(centerPanel);
        borderPanel.add(centerPanelConstrain, BorderLayout.CENTER);
    
        getContentPane().add(borderPanel);
        pack();
    }
    
    }
    

    See also this answer. There is more than one way to solve this.

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

Sidebar

Related Questions

We have a medium sized .js file that we include in our web framework
I have a medium sized application that runs as a .net web-service which I
I have two medium-sized web applications that I'm merging into one. They are both
I have a medium-sized process viewer which uses around ~40MB of private memory on
I have several medium-sized data sets in-memory that I need to be able to
I have a medium sized WinForm App ( 1 Form that hosts 40 user
I have the following JSON that I want to parse into C#. I am
I have a content management system that I have designed which uses XML files
Say I have the following data Name Value =============== Small 10 Medium 100 Large
I have a dropdown box with three values,small,medium and large.If the value is small,

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.