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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:10:10+00:00 2026-06-14T03:10:10+00:00

I use Jave Swing and the simplified hierarchy can be understood as JFrame ->

  • 0

I use Jave Swing and the simplified hierarchy can be understood as JFrame -> JPanel -> JLabel.

Now I have options of adding JLabel into JPanel:

  1. panel.add(label);
  2. panel.setLayout(new BorderLayout()); panel.add(label, BorderLayout.NORTH);

This is common configuration I am doing in both cases

panel.setOpaque(false);
panel.setBorder(BorderFactory.createLineBorder(Color.yellow));

Even though I keep all other code unchanged the following method call gives very different results:

    Point location = getLocationOnFrame(label);

I call this method to identify location of JLabel relative to JFrame:

    public Point getLocationOnFrame (JLabel label) {
        if (label == null) return null;

        try {
            Point location = label.getLocationOnScreen();
            System.out.println(location);
            Point frameOffset = mainFrame.getLocationOnScreen();
            System.out.println(frameOffset);
            location.translate(-frameOffset.x, -frameOffset.y);
            return location;
        } catch (IllegalComponentStateException e) {}

        return null;
    }

These are results from System.out.println() even though visiually the label appears in the same place.

java.awt.Point[x=578,y=305]
java.awt.Point[x=0,y=0]

java.awt.Point[x=224,y=300]
java.awt.Point[x=0,y=0]

It seems to me that in 2nd case the numbers come from the left-top corner of parent JPanel not JLabel itself.

Basically I am trying to use the code from 2nd case and get numbers from 1st.

  • 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-14T03:10:12+00:00Added an answer on June 14, 2026 at 3:10 am

    As to why you’re getting different results, I have absolutely no idea.

    However, I believe you’re making more work for your self…

    Point loc = label.getLocation();
    Window win = SwingUtilities.getWindowAncestor(label);
    Point wrPos = SwingUtilities.convertPoint(label, loc, win);
    

    Will convert the labels coordinates to the windows coordinate space for you.

    Know remember, the location of the frame is the top left corner of the frame (no, really it is ;)), but your label exists within the frame’s contentPane which is offset within the frames border, so depending on what you are trying to do, you may be better using the contentPane instead of the frame 😉

    This is a simple example I used to test the logic 😉

    public class TestLocation {
    
        public static void main(String[] args) {
            new TestLocation();
        }
    
        public TestLocation() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new LocationPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class LocationPane extends JPanel {
    
            private JLabel label;
    
            public LocationPane() {
                setLayout(new GridBagLayout());
                label = new JLabel("A Label");
                add(label);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
    
                // While this works, really, don't do this, in paint methods
                // it's expensive and will slow down your system ;)
                Point loc = label.getLocation();
                Point pos = label.getLocationOnScreen();
                Window win = SwingUtilities.getWindowAncestor(this);
                Point wPos = win.getLocationOnScreen();
                Point wLoc = win.getLocation();
    
                Point rPos = SwingUtilities.convertPoint(label, label.getLocation(), win);
                JRootPane rootPane = SwingUtilities.getRootPane(this);
                Point cPos = SwingUtilities.convertPoint(label, loc, rootPane.getContentPane());
                Point wrPos = SwingUtilities.convertPoint(label, loc, win);
    
                FontMetrics fm = g2d.getFontMetrics();
                int rowHeight = fm.getHeight();
    
                String[] messages = new String[]{
                    "Label.location = " + loc.x + "x" + loc.y,
    //                "Label.locationOnScreen = " + pos.x + "x" + pos.y,
    //                "Window.location = " + wLoc.x + "x" + wLoc.y,
    //                "Window.locationOnScreen = " + wPos.x + "x" + wPos.y,
                    "RelativeTo.Window = " + wrPos.x + "x" + wrPos.y,
                    "RelativeTo.RootPane = " + rPos.x + "x" + rPos.y,
                    "RelativeTo.ContentPane = " + cPos.x + "x" + cPos.y,
                };
    
                int y = 0;
                for (String msg : messages) {
                    g2d.drawString(msg, 0, y + fm.getAscent());
                    y += rowHeight;
                }
    
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

use I can't divide into segads. As for my above example if 5 threads
Use Elixir and have two entities -- Voter and Candidate -- with many to
use javascript or other method ? have any recommendation?
Use case: You have 2-3 files displayed in your MacVim window(s). You press ctrl+Q
Use scenario is pretty simple: I have a desktop only application that could be
I always use Jave EE 6 framework for web application. So I am not
Use case is to have a server connect to thousands of users email accounts
use case example I have a servlet that is receiving login requests. If a
I have a jave program that serializes files that are stored and read later.
Use OPENXML to get dt element in MSSQL 2005. How can I get xmlns:dt

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.