Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:17:12+00:00 2026-06-09T03:17:12+00:00

For some reason i am having problems centering my panel vertically that is located

  • 0

For some reason i am having problems centering my panel vertically that is located inside another panel. I do exactly as the examples i studied but still no luck.

Down there is my code. Despite using setAlignmentY(0.5f) on my container panel, it still wont center when i resize the window.

Also the components inside container panel wont center either, despite setAligenmentX(0.5f).

I wonder if there is a solution for this, I pretty much tried everything out there but couldnt find a solution.

JLabel idLabel;
    JLabel passLabel;
    JTextField id;
    JTextField pass;
    JButton enter;
    JPanel container;

    public JournalLogin()
    { 
        //setLayout(new FlowLayout());
        //setPreferredSize(new Dimension(500, 500));
        //setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));


        container = new JPanel();
        container.setLayout(new MigLayout());
        container.setAlignmentX(0.5f);
        container.setAlignmentY(0.5f);
        container.setPreferredSize(new Dimension(300, 300));
        container.setBorder(BorderFactory.createTitledBorder("Login"));
        add(container);

        idLabel = new JLabel("ID:");
        idLabel.setAlignmentX(0.5f);
        container.add(idLabel);

        id = new JTextField();
        id.setText("id");
        id.setAlignmentX(0.5f);
        id.setPreferredSize(new Dimension(80, 20));
        container.add(id, "wrap"); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:17:13+00:00Added an answer on June 9, 2026 at 3:17 am

    setAlignmentX and Y are not the way to go about doing this. One way to center a component in a container is to have the container use GridBagLayout and to add the component without using any GridBagConstraints, a so-called default addition. There are other ways as well.

    For example to alter Nick Rippe’s example (1+ to him):

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    
    import javax.swing.*;
    
    public class UpdatePane2 extends JPanel {
       private static final int PREF_W = 300;
       private static final int PREF_H = 200;
    
       public UpdatePane2() {
          JPanel innerPanel = new JPanel();
          innerPanel.setLayout(new BorderLayout());
          innerPanel.add(new JLabel("Hi Mom", SwingConstants.CENTER), 
                BorderLayout.NORTH);
          innerPanel.add(new JButton("Click Me"), BorderLayout.CENTER);
    
          setLayout(new GridBagLayout());
          add(innerPanel);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("UpdatePane2");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new UpdatePane2());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should'nt be that difficult but for some reason i'm having trouble with this.
this Android code worked fine before, but i'm having problems for some reason. here
First time cake user and I'm having real apache problems. For some reason the
Kick me if I'm being silly but some some reason I'm having a heck
My Javascript is quite basic, but for some reason I am having trouble. I
This is almost certainly a very silly question, but for some reason I'm having
For some reason I've having problems reading this session variable within an ajax document,
I'm having problems installing Rails on OS X Lion for some reason... Installing under
I'm having problems using a ostream in my logger class. For some reason, the
Hi I am having some problems with apc caching. For some reason when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.