aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes x,y of buttons ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "x,y of buttons ?" Watch "x,y of buttons ?" New topic
Author

x,y of buttons ?

jrookie
Greenhorn

Joined: Aug 20, 2002
Posts: 17
hello
i wanna get coordinates of button in Applet. I use getLocation for each button but
it's always return equal data for all buttons (like "x=25 y=126").What's wrong ?
here is code
import java.util.*;
import java.io.*;
import java.applet.*;
import java.awt.Component.*;
public class ResumeApplet extends Applet implements ActionListener
{
Button b1,b2,b3;
Point p;

public void init()
{
b1=new Button("b1");
b2=new Button("b2");
b3=new Button("b3");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
add(b1);
add(b2);
add(b3);
}

public void actionPerformed(ActionEvent e)
{
Object obj;
obj=e.getSource();
if (obj==b1)
{
p=b1.getLocation();
System.out.println("b1: x="+p.x+" y="+p.y);
}
if (obj==b2)
{
p=common.getLocation();
System.out.println("b1: x="+p.x+" y="+p.y);
}
if (obj==b3)
{
p=common.getLocation();
System.out.println("b1: x="+p.x+" y="+p.y);
}
}

public void paint(Graphics g)
{
}
}
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
from the API doc of Component.getLocation()

Gets the location of this component in the form of a point specifying the component's top-left corner. The location will be relative to the parent's coordinate space.
Due to the asynchronous nature of native event handling, this method can return outdated values (for instance, after several calls of setLocation() in rapid succession). For this reason, the recommended method of obtaining a component's position is within java.awt.event.ComponentListener.componentMoved(), which is called after the operating system has finished moving the component.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

"jrookie" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: x,y of buttons ?