• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

x,y of buttons ?

 
jrookie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic