iMadz
iMadz

Reputation: 41

Centering components and resizing JFrame in swing

I am making a java GUI in which I have some vertical boxes. Inside those boxes, there are some buttons and Labels. I am trying to put the buttons and labels in the center but doesn't work! I am using this code to set the label in the center.

JLabel update = new JLabel("update");
update.setHorizontalTextPosition(CENTER);

where update is the last component of my vertical box.

The other problem is that I need the window to resize automatically depending on the changes in my GUI (since it is a dynamic one)! How can I make this too?

Upvotes: 0

Views: 451

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

I am trying to put the buttons and labels in the center but doesn't work! I am using this code to set the label in the center.

There are several ways to do this, but the easiest for me is to use a GridBagLayout. If the boxes/container (which hopefully extend from JPanel or JComponent) uses a GridBagLayout, and you add components into the container with GridBagConstraints: gridX and gridY set, but with weightX and weightY set to default of 0, those added components will center in the container.

I can't show code since I have no knowledge of the code you're currently using or the images of your observed/desired GUI's. If you need more help, please edit your question and provide more pertinent information.

The other problem is that I need the window to resize automatically depending on the changes in my GUI (since it is a dynamic one)! How can I make this too?

This will all depend on the layout managers that your GUI is using, something that we have no knowledge of as yet. Again, if you're still stuck, please create and post your Minimal, Complete, and Verifiable Example Program.

For example the following resizable GUI with centered buttons and JLabel texts:

enter image description here

Is created by the following code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class VertBoxes extends JPanel {
    private static final String[] LABEL_TEXTS = { "A", "One", "Two", "Monday",
            "Tuesday", "January", "Fourth of July",
            "Four score and seven years ago" };
    public static final int PREF_W = 260;
    public static final int PREF_H = 80;

    public VertBoxes() {
        setLayout(new GridLayout(0, 1, 5, 5));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        for (String labelTxt : LABEL_TEXTS) {
            add(new InnerBox(labelTxt));
        }
    }

    private class InnerBox extends JPanel {
        public InnerBox(String labelTxt) {
            setLayout(new GridBagLayout());
            setBorder(BorderFactory.createLineBorder(Color.black, 4));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(new JButton("Button"), gbc);
            gbc.gridy++;
            add(new JLabel(labelTxt), gbc);
        }

        @Override
        public Dimension getPreferredSize() {
            if (isPreferredSizeSet()) {
                return super.getPreferredSize();
            }
            return new Dimension(PREF_W, PREF_H);
        }
    }

    private static void createAndShowGui() {
        VertBoxes mainPanel = new VertBoxes();

        JFrame frame = new JFrame("Vertical Boxes");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

Upvotes: 3

Related Questions