0

I'm having trouble positioning elements in a JPanel. I tried using GridBagLayout but that doesn't seem to make any of the parts of the panel GUI components move. What should I do?

    panel.add(Label);
    panel.add(TextField);

    panel.add(Label);
    panel.add(JChooser);

Nothing seems to help move these GUI elements. They just act like they are in a FlowLayout. What should I do? I'm using a CardLayout for another panel (that panel holds other panels like this one in it), but this panel, I need to align them to the left.

The Label and TextField need to be on the same line, but the Label and JChooser need to be on a different line.

Example:

SomeLabel :  [    TextField      ]
SomeLabel :  [Chooser] 

I hope I explained well enough.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Matt
  • 51
  • 5

2 Answers2

1

Take a closer look at Laying Out Components Within a Container and How to Use GridBagLayout

GridBagLayout requires constraints, which define how and where a component will be positioned within the virtual grid maintained by it

Layout

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

    public static void main(String[] args) {
        new TestLayout();
    }

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(4, 4, 4, 4);
            add(new JLabel("SomeLabel :"), gbc);
            gbc.gridy++;
            add(new JLabel("SomeLabel :"), gbc);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.fill = GridBagConstraints.BOTH;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JComboBox(new Object[]{"Puppies", "Kittens"}), gbc);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

To set elements in JPanel use one of LayoutManagers.

If you want to set components free from location.

then use following to remove layout.

 panel.setLayout(null); 

After this you can call setBounds method to set bounds for component, or setLocation .

comp.setLocation(int left, int top); 

comp.setBounds(int left, int top, int width, int height); 
  • 1
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. Check out [Why is it frowned upon to use a null layout in SWING?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more details – MadProgrammer Oct 30 '14 at 00:30