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 210355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:59:51+00:00 2026-05-11T17:59:51+00:00

I have some troubles with positioning my label/password field. With this code they both

  • 0

I have some troubles with positioning my label/password field.
With this code they both get positioned in the center next to each other, while I actually want them in the middle of my panel on top of each other.

Does anyone know how I should do that?

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class Paneel_Pincode extends JPanel {


    Paneel_Pincode() {
        setLayout(new FlowLayout());

        JPasswordField pincode = new JPasswordField(15);
        pincode.setLocation(500, 500);
        JLabel pinInvoer = new JLabel();

        ImageIcon pin1 = new ImageIcon("images/voerPincodeIn.jpg");

        pinInvoer.setIcon(pin1);
        pinInvoer.setLocation(500,700);

        add(pincode);
        add(pinInvoer);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1000,1000);
        f.setLocationRelativeTo(null);

        f.add(new Paneel_Pincode());
        f.setVisible(true);
    }

}
  • 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-05-11T17:59:52+00:00Added an answer on May 11, 2026 at 5:59 pm

    To get the hang of layouts, I’d recommend reading my article on them (http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/). It’s old, but the concepts and how FlowLayout works are detailed.

    What do you mean by “on top of each other”?

    If you mean like

       Password
       <field>
    

    EDIT: I REMEMBERED AN EASIER WAY TO DO THIS (completely in the JDK/JRE)…
    (This is similar to what I’m doing with the BoxBeans below, but you don’t need the BoxBeans. I created BoxBeans to be able to use BoxLayout in a UI builder a long time ago…)

    JLabel label = new JLabel("Password") {
        @Override public Dimension getMaximumSize() {
            return super.getPreferredSize();
        }
    };
    JPasswordField field = new JPasswordField() {
        @Override public Dimension getMaximumSize() {
            return super.getPreferredSize();
        }
    };
    field.setColumns(10);
    Box verticalBox = Box.createVerticalBox();
    verticalBox.add(Box.createVerticalGlue());
    verticalBox.add(label);
    verticalBox.add(field);
    verticalBox.add(Box.createVerticalGlue());
    //
    Box horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(Box.createHorizontalGlue());
    horizontalBox.add(verticalBox);
    horizontalBox.add(Box.createHorizontalGlue());
    add(horizontalBox);
    

    Previous answer for reference…

    I DO NOT RECOMMEND THE FOLLOWING BUT IT MAY HELP OTHER READERS WITH IDEAS

    You can do something like

    setLayout(FlowLayout());
    JPanel group = new JPanel(new BorderLayout());
    group.add(new JLabel("Password"), BorderLayout.NORTH);
    group.add(passwordField, BorderLayout.SOUTH);
    add(group);
    

    This will create a little panel in the top-center of the overall UI that contains the Password and field.

    Note that the nested BorderLayout ensures that the label and field each get their preferred size. You’ll need to call setColumns on the field to the number of chars you’d like displayed.

    If you want to center the label/field vertically as well, you could do the following

    setLayout(new GridBagLayout());
    //
    add(new JLabel("Password"), 
        new GridBagConstraints(0,0,1,1,1,1,
            GridBagConstraints.SOUTH,GridBagConstraints.NONE, 
            new Insets(3,3,3,3), 0,0));
    field.setColumns(10);
    add(field, new GridBagConstraints(0,1,1,1,1,1,
        GridBagConstraints.NORTH,GridBagConstraints.NONE, 
        new Insets(3,3,3,3), 0,0));
    

    I hate using GridBagLayout in general, so I’ll add a version using BoxLayout (but it’s a bit trickier due to the preferred size settings)

        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        //
        JPanel stuffH = new JPanel();
        f.add(stuffH, BorderLayout.CENTER);
        stuffH.setLayout(new BoxLayout(stuffH, BoxLayout.X_AXIS));
        //
        JPanel stuffV = new JPanel();
        stuffV.setLayout(new BoxLayout(stuffV, BoxLayout.Y_AXIS));
        //
        JLabel label = new JLabel("Password");
        BoxAdapter labelAdapter = new BoxAdapter();
        labelAdapter.add(label);
        JPasswordField field = new JPasswordField();
        field.setColumns(10);
        BoxAdapter fieldAdapter = new BoxAdapter();
        fieldAdapter.add(field);
        //
        stuffV.add(new VerticalGlue()); // for vertical spacing
        stuffV.add(labelAdapter);
        stuffV.add(fieldAdapter);
        stuffV.add(new VerticalGlue()); // for vertical spacing
        //
        stuffH.add(new HorizontalGlue()); // for horizontal spacing
        stuffH.add(stuffV);
        stuffH.add(new HorizontalGlue()); // for horizontal spacing
        //
        f.setVisible(true);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    

    A few notes on this:

    • I’m using my BoxBeans helper classes – see http://javadude.com/tools/boxbeans. This page is based on VisualAge for Java, but the jar at the bottom of the page can be used outside VAJ. I just tried it in eclipse, for example.
    • AFAICS, you cannot set a jframe’s layout directly to BoxLayout, so I added an extra panel in between. There’s a check in BoxLayout that has trouble with the automatic indirection of the content pane.
    • I nested the BoxLayouts so there’s horizontal centering (the stuffH panel) containing a vertical centering (the stuffV panel). They are centered by surrounding them with “Glue” components, which are simply components that allow themselves to expand.
    • I had to put the label and field in a BoxAdapter which limits their maximum size to their preferred size. If you don’t want to use BoxAdapter, you can acheive the same effect by using the following for the field and label:

      JLabel label = new JLabel("Password") {
          @Override public Dimension getMaximumSize() {
              return super.getPreferredSize();
          }
      };
      JPasswordField field = new JPasswordField() {
          @Override public Dimension getMaximumSize() {
              return super.getPreferredSize();
          }
      };
      

    Hope this proves helpful to you and anyone else!
    — Scott

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 129k
  • Answers 129k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You get the following error because your user id has… May 12, 2026 at 5:50 am
  • Editorial Team
    Editorial Team added an answer I don't see why this isn't possible. You annotate the… May 12, 2026 at 5:50 am
  • Editorial Team
    Editorial Team added an answer This is an excerpt from Ext.Ajax.request documentation: isUpload : Boolean… May 12, 2026 at 5:50 am

Related Questions

Looking for some direction here as I'm running into some migration problems. We have
I have some HTML that looks like this: <ul class=faq> <li class=open> <a class=question
I'm having some troubles with Flex with regards to changing controls on different viewstack
I'm having some troubles with using the std::sort algorithm here. I was reading that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.