title: how to center a group of components in a JPanel regardless of its size
body: <p>I am new to java and i´m coding a login frame which, after successfully loggin in ( to a mysql database (localhost using xampp) ) closes itsself and opens the main frame. </p>
  
  <p>My Login screen has a welcome label, a usernamelabel, a passwordlabel, a textfield and a password field and a login button. i want those components to be centered in the window and stay centered and not changing their size regarless how large the jpanel becomes(by dragging it bigger or smaller once its on the screen). </p>
  
  <p>My Question is how do i get them to stay centered and adapt their location to the panels size if it changes ?? 
  Lets say the panels size is 100*100 and a button is at 50/50 and the panel changes to 200/200, but the button remains at 50/50 and if i set it resizeable horizontal or vertical it just grows bigger, but i want it to remain the same size just adapt its location to 100/100.</p>
  
  <p>I cant post images of my frames cause as it seems this requires reputations which i dont have cause i just created my account. I hope you can picture what i mean to do and what i have now.</p>
  
  <p>My code for the LoginFrame using GroupLayout is the following(i left the actionlistener out cause its not relevant) :</p>
  
  <pre><code>public LoginFrame() {
       setTitle("LoginFrame");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 363, 270);
       contentPane = new JPanel();
       contentPane.setBackground(Color.ORANGE);
       contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
       setContentPane(contentPane);
  
       JLabel lblWelcome = new JLabel("Welcome");
  
       JLabel lblNewLabel = new JLabel("Username: ");
  
       textField = new JTextField();
       textField.setColumns(10);
  
       JLabel lblNewLabel_1 = new JLabel("Password: ");
  
       passwordField = new JPasswordField();
  
       JButton btnLogin = new JButton("Login");
  
          GroupLayout gl_contentPane = new GroupLayout(contentPane);
          gl_contentPane.setHorizontalGroup(
          gl_contentPane.createParallelGroup(Alignment.LEADING)
              .addGroup(gl_contentPane.createSequentialGroup()
                  .addGap(107)
                  .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                      .addComponent(lblNewLabel)
                      .addComponent(lblNewLabel_1))
                  .addPreferredGap(ComponentPlacement.RELATED)
                  .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                      .addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                      .addComponent(textField, GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE))
                  .addGap(75))
              .addGroup(gl_contentPane.createSequentialGroup()
                  .addGap(145)
                  .addComponent(btnLogin)
                  .addContainerGap(145, Short.MAX_VALUE))
              .addGroup(gl_contentPane.createSequentialGroup()
                  .addGap(149)
                  .addComponent(lblWelcome)
                  .addContainerGap(155, Short.MAX_VALUE))
      );
      gl_contentPane.setVerticalGroup(
          gl_contentPane.createParallelGroup(Alignment.LEADING)
              .addGroup(gl_contentPane.createSequentialGroup()
                  .addGap(56)
                  .addComponent(lblWelcome)
                  .addGap(18)
                  .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                      .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                      .addComponent(lblNewLabel))
                  .addPreferredGap(ComponentPlacement.RELATED)
                  .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                      .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                      .addComponent(lblNewLabel_1))
                  .addPreferredGap(ComponentPlacement.UNRELATED)
                  .addComponent(btnLogin)
                  .addContainerGap(64, Short.MAX_VALUE))
      );
      contentPane.setLayout(gl_contentPane);
   }
  </code></pre>
  
  <p>I hope i get some helpful answers on this, i tried to be specific, tell me if anything´s missing. Thanks</p>