0

I have below code. But I want all the components to appear in the center of the JPanel. I have already spent countless hours on this but failed every time. Can someone please help?

package tg.com.bugtracker;

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class LoginPanel extends JPanel {    
    LoginPanel() {
        GridBagLayout layout = new GridBagLayout();
        setLayout(layout);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.LAST_LINE_END;
        add(new JLabel("Username"), constraints);
        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.LAST_LINE_START;
        JComboBox<String> combobox = new JComboBox<>();
        combobox.setPreferredSize(new Dimension(250, 20));
        add(combobox, constraints);
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.FIRST_LINE_END;
        add(new JLabel("Password"), constraints);
        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.FIRST_LINE_END;
        JTextField textfield = new JTextField();
        textfield.setPreferredSize(new Dimension(250, 20));
        add(textfield, constraints);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Bharat Nanwani
  • 653
  • 4
  • 11
  • 27
  • so the layout is not as you expected? does it not display at the centre. code looks good to me though.. – Pro May 20 '15 at 14:03
  • One fairly easy way to center a group of components in an area of a GUI is to lay them out in a panel & create a (or another) panel with `GridBagLayout`. Then place the panel with the components into the GBL as a single component with no constraint. Job done! – Andrew Thompson May 20 '15 at 14:08
  • They are in center but not how anyone would expect it. You know how a Log-in page for any application look? Right in the center of the entire panel! I'm so lost. I'm trying the setBounds(), I'll see if that works. – Bharat Nanwani May 20 '15 at 14:11
  • As an aside, when I created an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) of that code, I dropped an instance of `LoginPanel` into the `CENTER` of a `BorderLayout` and it sure seemed to be centered. See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) The size of a text field can be suggested by the number of columns and the font size. A combo box will take the size of the content, which comes down to the widest combo item and the renderer used. – Andrew Thompson May 20 '15 at 14:19
  • *"I'm trying the setBounds(), I'll see if that works."* Take it from me. It won't. Or even if it works on your system at default size, it will not work after resizing and will also likely fail on other systems (from the start). – Andrew Thompson May 20 '15 at 14:21
  • I'm doing what you suggested. I'm going to drop an instance of loginPanel on an JPanel with BorderLayout. – Bharat Nanwani May 20 '15 at 14:24
  • OMG!!! It freaking worked like a charm. I love you Andrew. – Bharat Nanwani May 20 '15 at 14:27

2 Answers2

1

One fairly easy way to center a group of components in an area of a GUI is to lay them out in a panel & create a (or another) panel with GridBagLayout. Then place the panel with the components into the GBL as a single component with no constraint. Job done!


As an aside, when I created an MCVE (Minimal Complete Verifiable Example) of that code, I dropped an instance of LoginPanel into the CENTER of a BorderLayout and it sure seems to be centered.

General tip

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)

The size of a text field can be suggested by the number of columns and the font size. A combo box will take the size of the content, which comes down to the widest combo item and the renderer used.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Ok, so first of all I quite don't understand your question, I think you want to put them in the center, but I don't know why you're using anchors. The anchors you're using will make the objects appear in the Line End, or in the Line Start, but you want them in the center so you should use:

constraints.anchor = GridBagConstraints.CENTER; //BIG MOTHER OF GOD REDUNDANCE

Which is a redundance because the default value for anchor is Center, and you shouldn't use setBounds, and I don't know why you are setting the size, the grid will always be of the size of the largest object in the column. If what you're trying to do there is fill the grid you should use:

constraints.fill = GridBagConstraints.HORIZONTAL;

Or vertical or Both depending the direction where you want to fill it. So if you want to all be displayed at the Center here's your "corrected code":

package tg.com.bugtracker;

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class LoginPanel extends JPanel {    
    LoginPanel() {
        GridBagLayout layout = new GridBagLayout();
        setLayout(layout);
        GridBagConstraints constraints = new GridBagConstraints(); 
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.CENTER; //Redundance but I leaved it for you to see the change.
        add(new JLabel("Username"), constraints);
        constraints.gridx = 1;
        constraints.gridy = 0;
        JComboBox<String> combobox = new JComboBox<>();
        combobox.setPreferredSize(new Dimension(250, 20));
        add(combobox, constraints);
        constraints.gridx = 0;
        constraints.gridy = 1;
        add(new JLabel("Password"), constraints);
        constraints.gridx = 1;
        constraints.gridy = 1;
        JTextField textfield = new JTextField();
        textfield.setPreferredSize(new Dimension(250, 20));
        add(textfield, constraints);

    }
}

Now all the objects should appear in the center, I recommend you this post here in Stack Overflow about GridBagLayout, is quite interesting and it has common errors and covers almost all about this awesome layout.

Community
  • 1
  • 1
Antonio Aguilar
  • 532
  • 1
  • 6
  • 14