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

  • SEARCH
  • Home
  • 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 8590273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:15:28+00:00 2026-06-11T23:15:28+00:00

I have put some JPanels into another JPanel which its’ layout is Box Layout

  • 0

I have put some JPanels into another JPanel which its’ layout is Box Layout and Y-Axis.
After I have put all the panels I need to get the Y position of each added JPanel in from the JPanel container panel. But, when I try to get that I always get zero for Y position of each JPanel. Please tell me how to get the Y position of each Jpanel from the JPanel container JPanel.

This is what I have done,

JPanel panelHolder = new JPanel(new BoxLayout(this, BoxLayout.Y_AXIS));
for(int i = 0; i< 10; i++){
   JPanel p = new JPanel(); 
   panelHolder.add(p);
}

int componentCount = panelHolder.getComponentCount();

for (int j = 0; i < componentCount ; j++) {

  Component c = pnlRowHolder.getComponent(i);
  JPanel p = (JPanel)c;
  System.out.println(p.getY());//I get zero for all
}
  • 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-11T23:15:29+00:00Added an answer on June 11, 2026 at 11:15 pm

    Add your JPanel to the JFrames contentPane thus allowing you to get the X and Y co ordinates using getX() and get() though I’d suggest adding all components first, as the points may change as more components are added and then as trashgod said simply call pack() on the frame instance.

    I did a short sample to demonstrate:

    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test().createAndShowUI();
                }
            });
        }
    
        private void createAndShowUI() {
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            initComponents(frame);
            
            frame.pack();//call pack 
            
            printPanelCompPoints(mainPanel);//produces correct coords
            
            frame.setVisible(true);
        }
        
        private JPanel mainPanel;
    
        private void initComponents(JFrame frame) {
            mainPanel = new JPanel(new BorderLayout());
            JPanel centerPanel = new JPanel();
            JPanel northPanel = new JPanel();
            JPanel southPanel = new JPanel();
            JPanel westPanel = new JPanel();
            JPanel eastPanel = new JPanel();
    
            centerPanel.add(new JLabel("CENTER"));
            northPanel.add(new JLabel("NORTH"));
            eastPanel.add(new JLabel("EAST"));
            southPanel.add(new JLabel("SOUTH"));
            westPanel.add(new JLabel("WEST"));
    
            mainPanel.add(centerPanel, BorderLayout.CENTER);
            mainPanel.add(northPanel, BorderLayout.NORTH);
            mainPanel.add(southPanel, BorderLayout.SOUTH);
            mainPanel.add(eastPanel, BorderLayout.EAST);
            mainPanel.add(westPanel, BorderLayout.WEST);
    
           frame.getContentPane().add(mainPanel);
        
           printPanelCompPoints(mainPanel);//produces all 0's
        }
    
        private void printPanelCompPoints(JPanel mainPanel) {
            for (int i = 0; i < mainPanel.getComponentCount(); i++) {
                System.out.println(mainPanel.getComponent(i).getX() + ", " + mainPanel.getComponent(i).getY());
            }
        }
    }
    

    As you can see calling printPanelCompPoints(mainPanel); in initComponents(JFrame frame) produces all 0’s, (as they have been added to the frame but `pack() has not been called).

    0, 0

    0, 0

    0, 0

    0, 0

    0, 0

    however in the createAndShowUI() after calling pack() on the JFrames instance calling printPanelCompPoints(mainPanel); produces the correct co-odrinates:

    44, 26

    0, 0

    0, 52

    99, 26

    0, 26

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

Sidebar

Related Questions

I have a Zend_Form_Element_Multicheckbox and I want to put some of its elements in
we have a cache which I would like to put some transaction scopes around
I have some code that I want to put into an svn repository for
I have text file with some stuff that i would like to put into
I have a restful webservice which receives some structured data which is put straight
hey this is so that I should just have put some infomation into the
All, I have put some System.out.println() statements in a web application's servlet init() ,
I'm trying to insert some spacers into a layout on JPanel . I've got
In my page I have put some links under which I don't want any
I have put some aliases in my .bashrc to open a group of project

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.