Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a JLabel on a JPanel

I'm using the NetBeans GUI builder to handle my layout (I'm terrible with LayoutManagers) and am trying to place a simple JLabel so that it is always centered (horizontally) inside its parent JPanel. Ideally, this would maintain true even if the JPanel was resized, but if that's a crazy amount of coding than it is sufficient to just be centered when the JPanel is first created.

I'm bad enough trying to handle layouts myself, but since the NetBeans GUI Builder autogenerates immutable code, it's been impossible for me to figure out how to do this centering, and I haven't been able to find anything online to help me.

Thanks to anybody who can steer me in the right direction!

like image 532
IAmYourFaja Avatar asked Aug 24 '11 18:08

IAmYourFaja


People also ask

Can you put a JLabel on a JPanel?

A JLabel describing each colour will be placed within each coloured JPanel (using the . add() command.)

How do you center something in a JFrame?

By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.

How do you change the alignment of a label in Java?

This can be done in two ways. To align to the right: JLabel label = new JLabel("Telephone", SwingConstants. RIGHT);

How do you center a JDialog?

If you really want to center a JDialog on screen, you can use code like this: // center a jdialog on screen JDialog d = new JDialog(); d. setSize(400, 300); d.


3 Answers

Here are four ways to center a component:

4 Centered Components

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

class CenterComponent {

    public static JLabel getLabel(String text) {
        return getLabel(text, SwingConstants.LEFT);
    }

    public static JLabel getLabel(String text, int alignment) {
        JLabel l = new JLabel(text, alignment);
        l.setBorder(new LineBorder(Color.RED, 2));
        return l;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel p = new JPanel(new GridLayout(2,2,4,4));
                p.setBackground(Color.black);
                p.setBorder(new EmptyBorder(4,4,4,4));

                JPanel border = new JPanel(new BorderLayout());
                border.add(getLabel(
                    "Border", SwingConstants.CENTER), BorderLayout.CENTER);
                p.add(border);

                JPanel gridbag = new JPanel(new GridBagLayout());
                gridbag.add(getLabel("GridBag"));
                p.add(gridbag);

                JPanel grid = new JPanel(new GridLayout());
                grid.add(getLabel("Grid", SwingConstants.CENTER));
                p.add(grid);

                // from @0verbose
                JPanel box = new JPanel();
                box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS ));

                box.add(Box.createHorizontalGlue());
                box.add(getLabel("Box"));
                box.add(Box.createHorizontalGlue());
                p.add(box);

                JFrame f = new JFrame("Streeeetch me..");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane(p);
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}
like image 137
Andrew Thompson Avatar answered Oct 17 '22 08:10

Andrew Thompson


By using Borderlayout, you can put any of JComponents to the CENTER area. For an example, see an answer to Stack Overflow question Get rid of the gap between JPanels. This should work.

like image 29
mKorbel Avatar answered Oct 17 '22 08:10

mKorbel


Even with BoxLayout you can achieve that:

JPanel listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.X_AXIS ));

JLabel label = new JLabel();
listPane.add(Box.createHorizontalGlue());
listPane.add(label);
listPane.add(Box.createHorizontalGlue());

mKorbel's solution is perfect for your goal. Anyway I always like to suggest BoxLayout because it's very flexible.

like image 11
Heisenbug Avatar answered Oct 17 '22 09:10

Heisenbug