21

I am trying to vertically align (center) both JLabels inside one JPanel.

JPanel panel = new JPanel();
panel.setPreferredSize(size);
JLabel label1 = new JLabel(icon);
JLabel label2 = new JLabel("text");
panel.add(label1);
panel.add(label2);

I have tried using setAligmentY() with no success. Both labels always appear on the top of JPanel.

UPD: Labels should be located next to each other like using FlowLayout, but in the middle of the JPanel.

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
  • 1
    I would suggest using [WindowBuilder](https://developers.google.com/java-dev-tools/wbpro/quick_start) – Kai Jul 06 '12 at 07:33
  • 1
    be sure to learn and understand the concept of LayoutManagers (see the corresponding chapter in the tutorial referenced in the Swing tag) - they are responsible for each and every aspect of .. well ... layout :-) The task is to find a manager which supports your requirement. BTW: never-ever use setXXSize, for reasons see http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519 – kleopatra Jul 06 '12 at 07:51
  • 1
    Possible duplicate of [add controls vertically instead of horizontally using flow layout](http://stackoverflow.com/questions/13510641/add-controls-vertically-instead-of-horizontally-using-flow-layout) – Abhijeet Feb 25 '16 at 06:38

3 Answers3

27

Use a GridBagLayout with the default constraints. Here is a small demo code:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestVerticalAlignement {

    protected void initUI() {
        final JFrame frame = new JFrame();
        frame.setTitle("Test vertical alignement");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JLabel label1 = new JLabel("label1");
        JLabel label2 = new JLabel("label2");
        panel.add(label1, gbc);
        panel.add(label2, gbc);
        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestVerticalAlignement().initUI();
            }
        });
    }

}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • @GuillaumePolet thanks for the quick answer. However, this is not exactly what I want. I got labels one under another, but I want label2 to be next to label1 like in FlowLayout, but inside JPanel to appear vertically in the middle. – Nikolay Kuznetsov Jul 06 '12 at 07:53
  • @NikolayKuznetsov My mistake, I understood something else. I updated the post. – Guillaume Polet Jul 06 '12 at 08:08
19

You can use this 2013 answer by Nakul Sudhakar:

I used a BoxLayout and set its second parameter as BoxLayout.Y_AXIS and it worked for me:

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
TylerH
  • 20,799
  • 66
  • 75
  • 101
Domenico Monaco
  • 1,236
  • 12
  • 21
4

Use gridlayout, simple. That should work.

Consider my following example:

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


/*
    <applet code=AJ07 width=450 height=450>
    </applet>
*/

    public class AJ07 extends JApplet{
        Container c=null;

        public void init(){

                JPanel pTop=new JPanel();
                JPanel pLeft=new JPanel();
                JPanel pCenter=new JPanel();
                JPanel pProperties=new JPanel();

                pLeft.setLayout(new GridLayout(20,1));

                c=this.getContentPane();
                JButton bNew=new JButton("New");
                pTop.add(bNew);
                JButton bOpen=new JButton("Open");
                pTop.add(bOpen);
                JButton bSave=new JButton("Save");
                pTop.add(bSave);
                JButton bSaveAll=new JButton("Save All");
                pTop.add(bSaveAll);
                JButton bRun=new JButton("Run");
                pTop.add(bRun);
                JButton bStop=new JButton("Stop");
                pTop.add(bStop);
                JButton bPause=new JButton("Pause");
                pTop.add(bPause);

                JButton bText=new JButton("TextBox");
                pLeft.add(bText);
                JButton bButton=new JButton("Button");
                pLeft.add(bButton);

                pProperties.setLayout(new GridLayout(20,1));
                pProperties.add(new Label("BackColor"));
                pProperties.add(new Label("ForeColor"));
                c.add(new TextArea(),BorderLayout.CENTER);

                c.add(pTop,BorderLayout.NORTH);
                c.add(pLeft,BorderLayout.WEST);
                c.add(new Label("Project Loaded Successfully!"),BorderLayout.SOUTH);
                c.add(pProperties,BorderLayout.EAST);
                //c.add(pCenter,BorderLayout.CENTER);
        }
    }

for which the output is as follows:

enter image description here

ave4496
  • 2,950
  • 3
  • 21
  • 48