I feel hard to understand how the LayoutManager work even the GUI is quite simple.
I trying to make the button place below the Tel No JTextField, but it placed beside Address JTextField , not below TelNo JTextField.

package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

public class OrderPage extends JPanel {

    public static void main(String[] args) {
        OrderPage order = new OrderPage();
        JFrame frame = new JFrame();
        frame = new JFrame();

    public void createAndShowGUI() {

        String[] labels = { "Name: ", "Address: ", "Tel No: " };
        int numPairs = labels.length;

        // Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        JPanel p1 = new JPanel(new BorderLayout());

        JButton button = new JButton("Confirm");
        p1.add(button, BorderLayout.SOUTH);

        for (int i = 0; i < numPairs; i++) {
            JLabel l = new JLabel(labels[i], JLabel.TRAILING);
            JTextField textField = new JTextField(25);

        // Lay out the panel.
        SpringUtilities.makeCompactGrid(p, numPairs, 2, // rows, cols
                6, 6, // initX, initY
                6, 6); // xPad, yPad

1 Week
Discussion Span
Last Post by scheppy

It looks like you have been experimenting with a variety of layout mangers!
Firstly - have you seen the Oracle tutorials - they are excellent. They start here: https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

In my experience they are all too simpistic for anything but the simplest of requirements except GridBagLayout and SpringLayout. SpringLayout was designed specifically to support drag'n'drop GUI builders and is a pain to work with at the code level.
GridBagLayout is the one I always go to when I have an interesting design, especially if I want it to behave in a particular way when porting across OSs or when the window is resized.

Of course you can nest JPanels with a box layout here and a flow or grid layout there, but controlling that on a resize is a nightmare. I suggest you jump straight to GridBag. Yes, there's a bit of a learning curve, but learn it once and you're set up for life!


Whats james said.. Grib bag layout.
I was accually just starting to learn this, or more it was on my list of things to learn. I had watched this video on youtube few days ago and it really helped me understand it alot better.. how the layout accually works and its coordinate system.

heres is the link
Click Here

You might want to use 1.25speed or more though:).

Note, When im not sure of somthing a concept or somthing new the first place I look is youtube before anything else! you will be amazed at the vast amount of information available on youtube on java programming. Is it all good? in terms of coding practice? not sure.. Great for concepts and how things work.

Good luck i hope it helps.

Edited by scheppy


Is it all good? in terms of coding practice? not sure

One thing to watch out for is the date when the video was made. I still see a lot of web sites showing code from before Java 1.5, and even more that have not been updated for the huge changes in Java 8. Oracle's tutorials are the best for being up to date.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.