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();
        order.createAndShowGUI();
        JFrame frame = new JFrame();
        frame = new JFrame();
        frame.setTitle("Details");
        frame.getContentPane().add(order);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    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);
            p.add(l);
            JTextField textField = new JTextField(25);
            l.setLabelFor(textField);
            p.add(textField);
        }
        add(p);
        add(p1);

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

    }
}

Recommended Answers

All 7 Replies

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.

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.

@JamesCherril
Good point.

Hey,
If a container holds components whose size is not affected by the container's size or by font, look-and-feel, or language changes, then absolute positioning might make sense. Desktop panes, which contain internal frames, are in this category. The size and position of internal frames does not depend directly on the desktop pane's size. The programmer determines the initial size and placement of internal frames within the desktop pane, and then the user can move or resize the frames. A layout manager is unnecessary in this situation.

Another situation in which absolute positioning might make sense is that of a custom container that performs size and position calculations that are particular to the container, and perhaps require knowledge of the container's specialized state. This is the situation with split panes.

Creating a container without a layout manager involves the following steps.

Set the container's layout manager to null by calling setLayout(null).
Call the Component class's setbounds method for each of the container's children.
Call the Component class's repaint method.
However, creating containers with absolutely positioned containers can cause problems if the window containing the container is resized.

Set the container's layout manager to null by calling setLayout(null).

Hi @Lokesh_8, I thought setting layout manager consider as bad practice ?

He referred to setting a null layout manager, which, as you say, is generally a bad idea in almost all circumstances. (Eg any time there is any text being displayed!)
But he was specifically discussing some of the special cases where a null LM could be appropriate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.