Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 625
Nikolay Kuznetsov Avatar asked Jul 06 '12 07:07

Nikolay Kuznetsov


People also ask

Is JPanel a swing?

JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar.

Can a JPanel contain another JPanel?

A JPanel is a container that is itself a JComponent. A JPanel can contain other components, and it can in turn be contained in another component. The fact that panels can contain other panels means that you can have many levels of components containing other components, as shown in the illustration on the right.

What is box layout in Java?

Box Layout Features. As said before, BoxLayout arranges components either on top of each other or in a row. As the box layout arranges components, it takes the components' alignments and minimum, preferred, and maximum sizes into account.

What is JPanel form?

The JPanel is a simplest container class. It provides space in which an application can attach any other component. It inherits the JComponents class. It doesn't have title bar.


3 Answers

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

}
like image 88
Guillaume Polet Avatar answered Oct 09 '22 00:10

Guillaume Polet


you can see this answer: https://stackoverflow.com/a/18073909/189411

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
like image 24
Domenico Monaco Avatar answered Oct 08 '22 23:10

Domenico Monaco


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

like image 3
Darshak Mehta Avatar answered Oct 08 '22 23:10

Darshak Mehta