Skip to main content

Finding components in a panel

4 replies [Last post]
javaswingus
Offline
Joined: 2006-02-17
Points: 0

Hello,
I have a Label at x=60 and y=60 inside a Panel. I need to find out components that are 25 pixels (surrounding)within the label. i.e surrounding the label on any side.

e.g I have label, label1 and label2.

label is at x=60 and y=60
label1 is at x=45 and y=45
label2 is at x=300 and y=300

I need to get components that are surrounding "label" at 25 pixels at any side.

Any help would be greatly appreciated.

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
javaswingus
Offline
Joined: 2006-02-17
Points: 0

Mikael,
I suppose I got the answer now. I was trying to print the components in the panel and not the components in the list. I am now getting the components that are present surrounding the required component.

A small problem here, Now I wanted to delete a component that is present surrounding the required
label. I have a component that will be present near every label in the panel.
I select the label and delete it and it deletes the component that is present
with it also. I am using the remove() method. And it works.

I wanted to delete only a particular label(arrow) if its present within the given
rectangle range. How do I do this. I can remove the arrow label too, but only for the
first time. Any ideas?

[code]
public void closeComponents()
{
Component[] comps = null;
Rectangle testRect = null;
LinkedList closeList = null;
Component cc = null;
//LaunchArrow arr;

for (int j=0; j < fl.size(); j++)
{
if ((LaunchLabel)fl.get(j) == mark)
{
cc =(LaunchLabel)fl.get(j);
}
}
for (int j=0; j < tl.size(); j++)
{
if ((LaunchLabel)tl.get(j) == mark)
{
cc = (LaunchLabel)tl.get(j);
}
}

closeList = new LinkedList();
comps = getComponents();
testRect = cc.getBounds();
testRect.grow(6, 6);

for (int i = 0; i < comps.length; i++)
{
if (comps[ i ] != cc && testRect.intersects(comps[ i ].getBounds()))
{
//closeList.add(comps[ i ]);
Component cd = (Component)comps[ i ];
System.out.println(cd);
//remove(comps[ i ]);
//System.out.println("close to:"+closeList);
}
}
/*if(closeList.contains(arrow))
{
remove(arrow);
}*/
//closeList.clear();
repaint();
}

[/code]
Thanks

mgrev
Offline
Joined: 2003-08-12
Points: 0

Try this, jst from the top of my head, havn't compiled it...

[code]
LinkedList closeList = new LinkedList();
Component[] comps = panel.getComponents();
Rectangle testRect = label.getBounds();
testRect.grow(25, 25);

for (int i = 0; i < comps.length; i++)
if (comps[ i ] != label && testRect.intersects(comps[ i ].getBounds())
closeList.add(comps[ i ]);
[/code]

The "close" components is in [code]closeList[/code]

Regards,
Mikael

Message was edited by: mgrev (code formatting, lost [ ])

javaswingus
Offline
Joined: 2006-02-17
Points: 0

Mikael,
Thank you for the reply. when I use the code, I am only able to get total components in the Panel. Here is the code that I am using

[code]
LaunchLabel la = new LaunchLabel(mark); //finds which label has been selected
String id = la.getName();
System.out.println(la.getBounds());
Component[] comps = null;
Rectangle testRect = null;
LinkedList closeList = null;

for(int k=0; k < getComponentCount(); k++)
{
closeList = new LinkedList();
comps = getComponents();
testRect = la.getBounds();
testRect.grow(5, 5);
}

for (int i = 0; i < comps.length; i++){
if (comps[ i ] != la && testRect.intersects(comps[ i ].getBounds()))
closeList.add(comps[ i ]);
System.out.println(comps[i]);
}
[/code]
I need to find out components that are present within 25pixels of radius of the selected label and not all the components in the panel. Any help?

Thanks

Anonymous

Use the method getComponentAt(int x, int y) method.

Thanks,
Jana