Java align JLabel in center of JPanel

57,528

Solution 1

As I see it you need the label to be displayed at its preferred size and then you need a left and right panel to equally fill the remaining space available in the window.

You can use the Relative Layout class.

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

public class RelativeSSCCE extends JPanel
{
    public RelativeSSCCE()
    {
        JPanel left = new JPanel( new FlowLayout(FlowLayout.LEFT) );
        left.add( new JButton("L1") );
        left.add( new JButton("L2") );
        left.add( new JButton("L3") );

        JLabel center = new JLabel("Centered");

        JPanel right = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
        right.add( new JButton("Right1") );
        right.add( new JButton("Right2") );
        right.add( new JButton("Right3") );

        // choose your layout manager here

        setLayout( new RelativeLayout() );
        Float ratio = new Float(1);
        add(left, ratio);
        add(center);
        add(right, ratio);
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Basic RelativeSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new RelativeSSCCE() );
        frame.setSize(600, 100);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Solution 2

You could also use a BoxLayout, with X_AXIS layout, and add to the container

Edit: Here's an example

panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(innerPanel);
panel.add(Box.createHorizontalGlue());

Solution 3

Use BorderLayout. Add the JLabel with the BorderLayout.CENTER constraint and add a pair of JPanels with the BorderLayout.EAST and BorderLayout.WEST constraints. Add your additional buttons to the appropriate JPanel.

To have the text appear centered, you also need to set the Label's horizontalAlignment to JLabel.CENTER.

Solution 4

setAlignmentX(java.awt.Component.CENTER_ALIGNMENT)

and

setAlignmentY(java.awt.Component.CENTER_ALIGNMENT) 

may help if used with the elements which have to be centered, e.g. JPanel

Share:
57,528
Bassetts
Author by

Bassetts

Updated on July 09, 2022

Comments

  • Bassetts
    Bassetts almost 2 years

    I have a bar at the top of my application that has a number of buttons either side of a JLabel. The button's visibility is dependent upon the current task a user is carrying out and one of the button's text may also change depending on the current state.

    What I would like to do is have a number of buttons stick to the left of the JPanel, the JLabel's center to be in the very center of the JPanel and the rest of the buttons to be to the right of the JPanel.

    So far I have managed to get the buttons sticking to the left and the right using various methods and layout managers but I cannot get the JLabel's center to be in the dead center of the JPanel. The best I have managed to get is the label near enough to the center but it would then move around as the buttons are set to visible or their text changes.

    Is it possible to force the JLabel to remain dead center?

    Note: the buttons will never become big enough to meet either edge of the JLabel, so that is not a problem.

  • Bassetts
    Bassetts over 13 years
    I have tried that but because the east and west panels are not equal in size, the number of buttons in the panels and the text on the buttons can change, the center is not actually centered in the main panel.
  • Bassetts
    Bassetts over 13 years
    I am yet to try the relative layout, but the GridBagLayout has the same problem as the BorderLayout. The left and right panels are not of equal widths and so the center panel is not truly centered.
  • Bassetts
    Bassetts over 13 years
    The RelativeLayout worked a treat, would have preferred to use a default layout manager but just looks like they can't handle what seems to be a trivial task. Thanks.
  • Czar Pino
    Czar Pino over 11 years
    Just found out that this only works when panel has its minimum & maximum size set to its preferred size.