开发者

How to getLocation() in JPanel of JLabel that has been dynamically added?

开发者 https://www.devze.com 2023-03-29 07:04 出处:网络
I have created a number of JLabels dynamically and have added them to a JPanel with the following code (code should be enough to understand the problem I hope!).

I have created a number of JLabels dynamically and have added them to a JPanel with the following code (code should be enough to understand the problem I hope!).

    JPanel textPanel = new JPanel();
    map = new HashMap<Integer,JLabel>();
    vec = new Vector<JLabel>();
    for(int i = 0; i<getCount();i++){ // getCount() returns int
               JLabel label = new JLabel(getItemText(i)); // getItemText() returns String
               map.put(i, label);
               vec.add(label);
               textPanel.add(map.get(i));
    }

Now I am trying to access the Location of these labels but get nothing but java.awt.Point[x=296,y=63] for them when trying to access them via the following code.

System.out.println("Component position [1]: " +
textPanel.getComponent(1).getLocationOnScreen());

I get the same position for all Components, not just for the one.

Also (more importantly) I get the position java.awt.Point[x=0,y=0] for the following code.

System.out.println("Position of Component 1: " + map.get(1).getLocation());

I'm guessing this is to do with the fact that the J开发者_JAVA技巧Labels are created dynamically. I really need to create them dynamically, however, and also really need to be able to get their Location via getLocation().

Please help! Perhaps there is another way to create them or a way to access their Location(s) differently?


When you create a component it has a default location of (0, 0);

Adding a component to a panel does NOT change this location.

After adding all the labels to the panel you need to do:

panel.revalidate();

This will invoke the layout manager used by the panel and each label will then be assigned a proper location based on the rules of the layout manager.


Here is an SSCCE.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class WhereIsMyComponent {

    public static void showComponentLocations(Container parent) {
        Component[] all = parent.getComponents();
        System.out.println("Show locations of children..");
        for (Component c : all) {
            System.out.println(c.getLocation());
        }
    }

    public static void main(String[] args) {
        String msg = "Hello World!";
        final JPanel p = new JPanel(new FlowLayout());
        for (int ii=0; ii<6; ii++) {
            p.add(new JLabel(msg));
        }

        ComponentListener cl = new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent ce) {
                showComponentLocations(p);
            }
        };

        p.addComponentListener(cl);

        JFrame f = new JFrame("Where Is My Component?");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p);
        f.pack();
        f.setSize(400,300);
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }
}

Typical output

Show locations of children..
java.awt.Point[x=16,y=5]
java.awt.Point[x=89,y=5]
java.awt.Point[x=162,y=5]
java.awt.Point[x=235,y=5]
java.awt.Point[x=308,y=5]
java.awt.Point[x=162,y=26]
Show locations of children..
java.awt.Point[x=16,y=5]
java.awt.Point[x=89,y=5]
java.awt.Point[x=162,y=5]
java.awt.Point[x=235,y=5]
java.awt.Point[x=308,y=5]
java.awt.Point[x=162,y=26]
Show locations of children..
java.awt.Point[x=26,y=5]
java.awt.Point[x=99,y=5]
java.awt.Point[x=172,y=5]
java.awt.Point[x=26,y=26]
java.awt.Point[x=99,y=26]
java.awt.Point[x=172,y=26]
Press any key to continue . . .
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号