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

But in actual I am looking to get this

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
From the Java trails of
SpringLayout(pretty much the first line, actually):I’ve been programming professionally in Java for years now and even I won’t program
SpringLayoutby 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’sBorderLayoutdue to the way space-filling works. I highly recommend it.First things first:
Here is an example using
MigLayoutto produce a similar layout to the example image you posted:And its output:
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 thatMigLayoutis 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
MigLayoutconstructor (here,"fillx") is a layout constraint, and anything declared in theaddmethods (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
GridLayoutorBoxLayout. 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:BorderLayoutBoxLayoutCardLayoutFlowLayoutGridBagLayoutGridLayout