wood burning stoves
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

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "x,y of buttons ?" Watch "x,y of buttons ?" New topic

x,y of buttons ?


Joined: Aug 20, 2002
Posts: 17
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");

public void actionPerformed(ActionEvent e)
Object obj;
if (obj==b1)
System.out.println("b1: x="+p.x+" y="+p.y);
if (obj==b2)
System.out.println("b1: x="+p.x+" y="+p.y);
if (obj==b3)
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

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!

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 ?
It's not a secret anymore!