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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:53:12+00:00 2026-06-12T11:53:12+00:00

I am trying to create a SpringLayout in Java, this is the code I

  • 0

I am trying to create a SpringLayout in Java, this is the code I have (got it from Oracle Java docs)

import javax.swing.*;
import java.awt.*;

public class SpringForm {

    private static void createAndShowGUI() {
        String[] labels = {"Side1: ", "Side2: ", "Side3: "};
        int numPairs = labels.length;
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        javax.swing.JButton calculate_btn;
        calculate_btn = new javax.swing.JButton();             

        for (int i = 0; i < numPairs; i++) {
            JLabel l = new JLabel(labels[i], JLabel.TRAILING);
            p.add(l);
            JTextField textField = new JTextField(10);
            l.setLabelFor(textField);
            p.add(textField);
        }
        calculate_btn.setText("Calculate");
        p.add(calculate_btn);
        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
            numPairs, 2, //rows, cols
            6, 6,        //initX, initY
            6, 6);       //xPad, yPad     
        //Create and set up the window.
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        //Set up the content pane.
        p.setOpaque(true);  //content panes must be opaque
        frame.setContentPane(p);     
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

With the above code I am getting this

enter image description here

But in actual I am looking to get this

enter image description here

I DONOT MIND ANY OTHER LAYOUT! it’s just that the oracle help shows that springlayout really close to my need
I am trying to do is get the layout as below picture, I am not sure what is the layout used in the below, and also in my attempt I am using SpringLayout I noticed that the controls change their sizes automatically when we extend the window size I want to disable this but it kind of doesn’t make sense as SpringLayout clearly means what it is doing, adjusting the controls, when we adjust the window

  • 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-12T11:53:13+00:00Added an answer on June 12, 2026 at 11:53 am

    From the Java trails of SpringLayout (pretty much the first line, actually):

    The SpringLayout class was added in JDK version 1.4 to support layout in GUI builders. SpringLayout is a very flexible layout manager that can emulate many of the features of other layout managers. SpringLayout is, however, very low-level and as such you really should only use it with a GUI builder, rather than attempting to code a spring layout manager by hand. (Emphasis added.)

    I’ve been programming professionally in Java for years now and even I won’t program SpringLayout by hand. My recommendation is to use the MigLayout library instead. Their API is much simpler for layout-by-hand code and can produce very near-native layouts. I’ve been using it for a long time now, and I prefer it over anything else I’ve tried. It’s especially nice when used in conjunction with Java’s BorderLayout due to the way space-filling works. I highly recommend it.


    First things first:

    1. MigLayout is cell-based, but also supports splitting and spanning cells. If you’ve ever worked with HTML or Excel, you should know what that means. It’s pretty self-explanatory.
    2. MigLayout’s default method of input is strings, and they’re the simplest to understand, but they also have a very good API for creating layouts as well.
    3. MigLayout supports far more than I’ll ever be able to cover in an SO question, so follow the link above and check out the quick start guide and cheat sheet. They’re by far the best resource for what you can put in your constraints.

    Here is an example using MigLayout to produce a similar layout to the example image you posted:

    public static void main(String[] args) {
        JFrame frame = new JFrame("Testing MigLayout");
        JPanel contentPane = new JPanel(new MigLayout("fillx"));
    
        // Row 1
        JLabel areaLabel = new JLabel("Area of Triangle");
        areaLabel.setFont(areaLabel.getFont().deriveFont(16.0f));
        areaLabel.setHorizontalAlignment(JLabel.CENTER);
        contentPane.add(areaLabel, "spanx, growx, wrap");
        // wrap indicates a new row
    
        // Row 2
        JLabel side1Label = new JLabel("Side 1:");
        contentPane.add(side1Label, "alignx trailing");
        JTextField side1Field = new JTextField();
        side1Field.setColumns(6);
        contentPane.add(side1Field, "alignx leading, wrap");
    
        // Row 3
        JLabel side2Label = new JLabel("Side 2:");
        contentPane.add(side2Label, "alignx trailing");
        JTextField side2Field = new JTextField();
        side2Field.setColumns(6);
        contentPane.add(side2Field, "alignx leading, wrap");
    
        // Row 4
        JLabel side3Label = new JLabel("Side 3:");
        contentPane.add(side3Label, "alignx trailing");
        JTextField side3Field = new JTextField();
        side3Field.setColumns(6);
        contentPane.add(side3Field, "alignx leading, wrap");
    
        // Row 5
        JButton calculateButton = new JButton("Calculate Area");
        contentPane.add(calculateButton, "spanx, growx");
    
        frame.setContentPane(contentPane);
        // Resizes automatically
        frame.pack();
        // Centers automatically
        frame.setLocationRelativeTo(null);
        // Exit when the frame is closed
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        frame.setVisible(true);
    }
    

    And its output:

    MigLayout Example

    This is without all the logic, of course, but it still shows MigLayout’s power. When you start getting into more complex applications and want components to expand and contract with the window, MigLayout does very well. If you’ve ever used GridBadLayout, you’ll notice that MigLayout is just a suped up version of it.

    For references about what all the individual pieces of this are, just look at the cheat sheet. There’s an explanation for every piece I used. Specifically, anything declared in the MigLayout constructor (here, "fillx") is a layout constraint, and anything declared in the add methods (such as "spanx" and "wrap") are component constraints. There’s more you can do with practice and experimentation to get just the right combination that creates an excellent GUI.

    That being said, there’s always other, simpler layout managers like GridLayout or BoxLayout. For simple applications like yours, those layout managers are perfectly fine. When you start getting into the more intensive applications, I recommend breaking into MigLayout. For reading up on those, I recommend the Java trails. There’s a visual guide to layouts on there as well, and you can use that as a jumping-off point. I recommend sticking with these for layout-by-hand:

    • BorderLayout
    • BoxLayout
    • CardLayout
    • FlowLayout
    • GridBagLayout
    • GridLayout
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying create empty white image with php, this s my code $bg =
I'm trying create new object from a module class in VBA, and I have
I am trying create a data.frame from which to create a graph. I have
I'm trying create simple application in C++. This application has to read from file
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create a background-image slideshow and am getting this error... This is the
I'm trying create a log specifically for logging user account activity. I created this
I'm trying create an immutable object and initialise it from xml config file in
I'm trying create a new file with a Java Applet, but I don't know
I trying create a class derivated from System.Web.UI.Page and in override Render i set

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.