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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:47:06+00:00 2026-05-17T20:47:06+00:00

I have a code: Box box_general = Box.createHorizontalBox(); Box box_panel1 = Box.createVerticalBox(); Box box_panel2

  • 0

I have a code:

    Box box_general = Box.createHorizontalBox();
    Box box_panel1 = Box.createVerticalBox();
    Box box_panel2 = Box.createVerticalBox();

    JPanel jpanel_1 = new JPanel();

    jpanel_1.setPreferredSize(new Dimension(50, 152));
    jpanel_1.setOpaque(true);
    jpanel_1.setBorder(BorderFactory.createLineBorder(Color.BLUE));

    JPanel jpanel_2 = new JPanel();

    jpanel_2.setPreferredSize(new Dimension(340, 152));
    jpanel_2.setOpaque(true);
    jpanel_2.setBorder(BorderFactory.createLineBorder(Color.RED));

    JTextField jtxtf_populationSize = new JTextField();
    jtxtf_populationSize.setSize(10, 20);
    JTextField jtxtf_processorsAmount = new JTextField();

    JButton jbtn_loadProcesses = new JButton("File path");
    box_panel1.add(Box.createRigidArea(new Dimension(0,4)));        
    box_panel1.add(jtxtf_processorsAmount);
    box_panel1.add(Box.createRigidArea(new Dimension(0,20)));       
    box_panel1.add(jbtn_loadProcesses);
    jpanel_1.add(box_panel1);

    JLabel jlbl_populationSize = new JLabel("Enter the population size");
    JLabel jlbl_processorsAmount = new JLabel("Enter the amount of processors");
    JLabel jlbl_loadProcesses = new JLabel("Load processes from file");

    jlbl_populationSize.setFont(font);
    jlbl_processorsAmount.setFont(font);
    jlbl_loadProcesses.setFont(font);

    box_panel2.add(jlbl_populationSize);
    box_panel2.add(Box.createRigidArea(new Dimension(0,4)));        
    box_panel2.add(jlbl_processorsAmount);
    box_panel2.add(Box.createRigidArea(new Dimension(0,15)));       
    box_panel2.add(jlbl_loadProcesses);
    jpanel_2.add(box_panel2);

    box_general.add(jpanel_2);
    box_general.add(Box.createRigidArea(new Dimension(10,0)));
    box_general.add(jpanel_1);

It creates 3 boxes, where the general box contains two other boxes. The problem is: in the box all the compopents are stretched of width. For example, there is a line jtxtf_populationSize.setSize(10, 20); but this text field is stretched in the box_panel1 on width. I tried to use a BoxLayout with it’s Alignment_X but this didn’t work.

Could you, please, advise me, what to do – how to avoid stretching?

  • 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-17T20:47:06+00:00Added an answer on May 17, 2026 at 8:47 pm

    Most of the swing layouts will use the preferred size and the minimum/maximum size over a call to setSize. The key here is to get the right preferred and minimums so that they don’t shrink too much, and then insert a strut (Box#createHorizontalStrut) to fill up space where you don’t want a component.

    With complex layouts like this, consider the SpringLayout, which admittedly has a higher learning curve, but once you get used to it, will allow you to more naturally state the constraints you want.

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

Sidebar

Related Questions

I have a list box control: <asp:ListBox runat=server id=lbox autoPostBack=true /> The code behind
I have code like this: var newMsg = new Msg { Var1 = var1,
I have this java code: <script src=http://www.google.com/jsapi></script> <script type=text/javascript> google.load(jquery, 1.2.6); $(a#more).click(function() { $(#info_box).show(blind,
I have code that references a web service, and I'd like the address of
I have code to create another row (div with inputs) on a button click.
I have code similar to this filtering entries in an Array of Objects: var
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code similar to the following in many places: var dbParams = db.ReadParams(memberID,
I have code like this to move the player in my game left, right,
I have code that looks like the following, which works fine for displaying the

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.