Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

Java: vertical alignment within JPanel

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.

Upvotes: 20

Views: 85510

Answers (3)

Domenico Monaco
Domenico Monaco

Reputation: 1236

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));

Upvotes: 19

Darshak Mehta
Darshak Mehta

Reputation: 41

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

Upvotes: 4

Guillaume Polet
Guillaume Polet

Reputation: 47637

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();
            }
        });
    }

}

Upvotes: 26

Related Questions