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

The Archive Base Latest Questions

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

I am facing this issue in another project which has complicated JPanels structure and

  • 0

I am facing this issue in another project which has complicated JPanels structure and much code.

In there I have a following snippet

Point point = label.getLocation();
Point point2 = SwingUtilities.convertPoint(label.getParent(), point, panel);

in where label and panel are selected dynamically and JLabel is supposed to be grand-grand-…-child of JPanel.

So why question is in what case SwingUtilities.convertPoint() can return negative values if source and destination have child-parent relation? Is this possible at all?

This code demonstrated negative coordinates issue, but there panel22 is not parent of label.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NegativeLocation {
    public static void main(String[] args) {
        new NegativeLocation();
    }

    public NegativeLocation() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                JPanel panel1 = new JPanel() {
                     @Override
                     public Dimension getPreferredSize() {
                         return new Dimension(600, 400);
                     }
                };

                JPanel panel21 = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(300, 400);
                    }
                };
                panel1.add(panel21);

                JPanel panel22 = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(300, 400);
                    }
                };
                panel1.add(panel22);

                JPanel panel3 = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(300, 400);
                    }
                };
                panel21.add(panel3);

                JLabel label = new JLabel("Label");
                panel3.add(label);

                frame.getContentPane().add(panel1);
                frame.setPreferredSize(new Dimension(600, 400));

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Point point = label.getLocation();
                Point point2 = SwingUtilities.convertPoint(label.getParent(), point, panel21);
                Point point3 = SwingUtilities.convertPoint(label.getParent(), point, panel22);

                System.out.println(point2);
                System.out.println(point3);
            }
        });
    }
}

So the output is

java.awt.Point[x=134,y=10]
java.awt.Point[x=134,y=-395]

UPD: Basically, I need to get location of label relative to panel21. How can I modify this line to achive it?

Point point = label.getLocation();
Point point2 = SwingUtilities.convertPoint(label.getParent(), point, panel21);
  • 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-15T12:44:29+00:00Added an answer on June 15, 2026 at 12:44 pm

    Take the following example…

    enter image description here

    public class TestLocation01 {
    
        public static void main(String[] args) {
            new TestLocation01();
        }
    
        public TestLocation01() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JPanel outter = new JPanel(new BorderLayout());
                    outter.setBorder(new CompoundBorder(new LineBorder(Color.RED), new EmptyBorder(10, 10, 10, 10)));
                    JPanel inner = new JPanel(new GridBagLayout());
                    inner.setBorder(new CompoundBorder(new LineBorder(Color.BLUE), new EmptyBorder(10, 10, 10, 10)));
                    JLabel label = new JLabel("Testing");
                    inner.add(label);
                    outter.add(inner);
    
                    JLabel below = new JLabel("Below");
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(outter);
                    frame.add(below, BorderLayout.SOUTH);
                    frame.setSize(200, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    System.out.println("lable.location: " + label.getLocation());
                    System.out.println("label to inner: " + SwingUtilities.convertPoint(label, new Point(0, 0), inner));
                    System.out.println("label to outter: " + SwingUtilities.convertPoint(label, new Point(0, 0), outter));
                    System.out.println("label.parent to inner: " + SwingUtilities.convertPoint(label.getParent(), new Point(0, 0), inner));
                    System.out.println("label.parent to outter: " + SwingUtilities.convertPoint(label.getParent(), new Point(0, 0), outter));
                    System.out.println("label to frame: " + SwingUtilities.convertPoint(label, new Point(0, 0), frame));
                    System.out.println("label.getParent to frame: " + SwingUtilities.convertPoint(label.getParent(), new Point(0, 0), frame));
                    System.out.println("outter to label: " + SwingUtilities.convertPoint(outter, new Point(0, 0), label));
                    System.out.println("label to below: " + SwingUtilities.convertPoint(label, new Point(0, 0), below));
                    System.out.println("below to label: " + SwingUtilities.convertPoint(below, new Point(0, 0), label));
                }
            });
        }        
    }
    

    Which generates (on my system) the following output

    lable.location: java.awt.Point[x=65,y=62]
    label to inner: java.awt.Point[x=65,y=62]
    label to outter: java.awt.Point[x=76,y=73]
    label.parent to inner: java.awt.Point[x=0,y=0]
    label.parent to outter: java.awt.Point[x=11,y=11]
    label to frame: java.awt.Point[x=76,y=95]
    label.getParent to frame: java.awt.Point[x=11,y=33]
    outter to label: java.awt.Point[x=-76,y=-73]
    label to below: java.awt.Point[x=76,y=-89]
    below to label: java.awt.Point[x=-76,y=89]
    

    If I pass the label as the source component, I get relative positions based upon the relationship between the label and the component whose coordinate space I’m trying to convert to. If I pass the label‘s parent, I’m using the parent coordinate space instead, which is bound to produce an incorrect result

    If you look at the relationship between label and below it makes sense to that you would get negative results, as label is above and to the right of below

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

Sidebar

Related Questions

I am facing an issue where I have one method which is calling another
I'm being bugged by this issue which I'm facing with location.replace . I need
I am facing an issue with SecureRandom in java. This was the code that
This is the code I am using to show the issue which I am
I have a method which calls a another method which inturns has some ajax
I am facing this issue whenever I am trying to run Play 1.2.1 in
I am facing this issue on Android 2.3.5 (and possibly earlier Android versions) When
I'm facing a problem to solve this issue. I'm having a string variable, for
Please help me with this issue that I am facing on production server. I
I didn't understand ..why I am facing this error.. Here is my code: package

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.