SCAREX
SCAREX

Reputation: 115

JFrame center components

I was wondering how to center my components, I found the gridBagLayout but I couldn't find a way to have it bigger than it is : my buttons are too small.

Part of my code :

private void addMainButtons() {
        JPanel container = new JPanel() {
            private static final long serialVersionUID = -424395301619105440L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Dimension dim = this.getSize();
                DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
                Date date = new Date();
                g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
                g.setColor(Color.BLACK);
                String s = format.format(date);
                g.drawString(s, (int) ((dim.getWidth() - s.length() - 80) / 2), 20);
            }
        };
        container.setBackground(new Color(109, 69, 60));

        JButton productsButton = new JButton("Produits");
        productsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(frame, pages[1]);
            }
        });
        productsButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        productsButton.setAlignmentX(Component.CENTER_ALIGNMENT);

        // Creating grid
        container.setPreferredSize(new Dimension(400, 600));
        container.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        container.add(productsButton, gbc);

        gbc.gridy = 1;
        gbc.gridwidth = 1;
        container.add(new JButton("Entrée"), gbc);

        gbc.gridx = 1;
        container.add(new JButton("Sortie"), gbc);

        mainPage.setLayout(new BorderLayout());
        mainPage.add(container, BorderLayout.CENTER);
        // End of main page
    }

GridBagLayout is really difficult to use, is there another way to center components ? I could use NestedLayouts but I don't know how.

Upvotes: 1

Views: 201

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347194

You can use weightx and/or weighty to effect the amount of space the components will occupy, for example, if I do

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;

gbc.weightx = 1;
gbc.weighty = 1;

container.add(productsButton, gbc);

gbc.gridy = 1;
gbc.gridwidth = 1;
container.add(new JButton("Entrée"), gbc);

gbc.gridx = 1;
container.add(new JButton("Sortie"), gbc);

I can generate this...

Big Buttons

You could use two containers, one holding a JLabel which shows the date and one which contains the buttons, but I doubt that's what you're really after.

You can use ipadx and ipady which adds the amount to the components preferred size

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;

gbc.ipadx = 40;
gbc.ipady = 40;

container.add(productsButton, gbc);

gbc.gridy = 1;
gbc.gridwidth = 1;
container.add(new JButton("Entrée"), gbc);

gbc.gridx = 1;
container.add(new JButton("Sortie"), gbc);

Not so big buttons

which, when included with your existing constraints, allows you to "grow" the buttons a bit.

Upvotes: 1

Puneet Chawla
Puneet Chawla

Reputation: 6009

Try below code for increasing size of button while adding every components to gridbagconstraints. Change the value of weightx/weighty as you want.

gbc.weightx = 1.0;
gbc.weighty = 1.0;

To give more spaces between components, use below code while adding every components to gridbagconstraints.

gbc.insets = new Insets(2, 2, 2, 2);

According to my knowledge, you used correct way to center components. It can also be done by other layout but it may be little bit difficult.

Upvotes: 0

Related Questions