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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:05:27+00:00 2026-05-23T10:05:27+00:00

What i’m trying to do In Swing, I’m trying to use a BoxLayout or

  • 0

What i’m trying to do

In Swing, I’m trying to use a BoxLayout or equivalent linear container, but the items in the container are stretching vertically. Inside my application, I don’t want them to stretch vertically.

I know i could set a preferredSize or maximumSize on components, but the following code is just a reproducer, and I can’t hard-code or maximize the size of the components, which are in reallity more complex and dynamic. And I just can’t use a BorderLayout with the BorderLayout.TOP position, because no scroll bars will ever show if I do that. And I might need scroll panes.


What I have tried

So I tried to use the fillers in a BoxLayout as explained in Using Invisible Components as Filler , but it just doesn’t work. Although in the Oracle documentation, it seemed to be exactly what I needed. Here are my attempts:

    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;


    public class TestBoxLayout implements Runnable {

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new TestBoxLayout());
        }

        @Override
        public void run() {
            JFrame f = new JFrame("test box layout");

            JPanel b = new JPanel();
            b.setLayout(new BoxLayout(b, BoxLayout.PAGE_AXIS));

            b.add(new JTextField("field 1"));
            b.add(new JTextField("field 2"));
            b.add(new JTextField("field 3"));
            b.add(Box.createVerticalGlue());

            f.setContentPane(b);
            f.setSize(500, 200);
            f.setVisible(true);
        }

    }

This is the result I get:

Fillers are not working


Second try

I tried to use the Box class instead of JPanel with BoxLayout, but the visual result is exactly the same. Here is my second try:

    import javax.swing.Box;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;


    public class TestBox implements Runnable {

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new TestBox());
        }

        @Override
        public void run() {
            JFrame f = new JFrame("test box");

            Box b = Box.createVerticalBox();

            b.add(new JTextField("field 1"));
            b.add(new JTextField("field 2"));
            b.add(new JTextField("field 3"));
            b.add(Box.createVerticalGlue());

            f.setContentPane(b);
            f.setSize(500, 200);
            f.setVisible(true);
        }

    }

What I would like to do

Does anyone know how I can fix those fillers and make them work ? A fix on the given code would be fantastic.

This is a drawing I made using paint, which shows what I’d like to have as a result:
Expected result

  • 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-23T10:05:27+00:00Added an answer on May 23, 2026 at 10:05 am

    BoxLayout is fine for what you are trying to do. The glue insert as last component is fine. Why aren’t simply using setMaximumSize methods on your JTextFields?
    I’m not sure JTextFields alone can satisfy your needs. If using setMaximumSize don’t work put each JTextField Inside a JPanel and then use setMaximumSize on each JPanel.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.