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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:24:52+00:00 2026-06-11T10:24:52+00:00

I have several JPanel s with which contain JLabel s and JTextField s for

  • 0

I have several JPanels with which contain JLabels and JTextFields for user input in my Swing application.

I am hand-editing the code generated by the NetBeans GUI Builder, but still want to maintain a similar layout.

I started by using a GridLayout. I can tweak the hgap to make my JLabels and JTextFields the right size, but I will have to do this individually for all of the JPanels.

Is there a standard LayoutManager which will calculate the correct height of a text component based on the component’s font size and pad the containing component with space in between and/or around the text components?

Edit:

As requested, here is an SSCCE to demonstrate what I am trying to do

GridLayoutSSCCE:

package gridlayoutsscce;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class GridLayoutSSCCE {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Grid Layout SSCCE");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new GridLayoutSSCCEPanel());
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

GridLayoutSSCCEPanel:

package gridlayoutsscce;

import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class GridLayoutSSCCEPanel extends JPanel {
    public GridLayoutSSCCEPanel() {
        this.setLayout(new GridLayout(2, 2));
        this.add(new JLabel("Label 1:"));
        this.add(new JTextField());

        this.add(new JLabel("Label 2:"));
        this.add(new JTextField());
    }
}

This looks fine when I first run the program. However, when I resize the window, the text fields stretch out so each fills half of the height of the window. I want them to remain the same height that they were originally when the window resizes.

  • 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-11T10:24:54+00:00Added an answer on June 11, 2026 at 10:24 am

    GridBagLayout will do what you want, although it is highly flexible, it also consider one of the most complex.

    I’m sure MigLayout will get a mention, but I’ve never personally used it, and I should also mention JGoodies FormLayout, but again, I’ve never used it.

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

Sidebar

Related Questions

I am creating a JPanel form which will contain several other JPanels. I want
I have a JPanel that has several (25) JLabel s on it, using a
I have a JTable that holds several JPanels using a custom renderer/editor. The JPanel
I am developing an application in Java, in my GUI I have several JPanels
I have an application with a lot of swing components, nested in several containers.
I have an JPanel populated with several opaque custom components. Now I would like
I have several listviews with two collumns like. I am building a stats application
I have several JComponent s on a JPanel and I want to disable all
I have a JPanel with several levels of child components, also with a JScrollPane.
I have a class that extends JPanel with several buttons on it. I would

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.