How to get the current X and Y position of a window?

M

Mark Sizzler

I can get the current height and width of a window by commands like:

this.getWidth()
and
this.getHeight()

But how do I get current X and Y position of this window?
Mark
 
D

dimka

I can get the current height and width of a window by commands like:

this.getWidth()
and
this.getHeight()

But how do I get current X and Y position of this window?
Mark š š š š š š š š š š š š š š š š š š

If you said about java.awt.Window, then you can use:
Window.getGraphicsConfiguration().getBounds();
 
J

John B. Matthews

I can get the current height and width of a window by commands like:

this.getWidth() and this.getHeight()

But how do I get current X and Y position of this window?

Try SwingUtilities#convertPointToScreen():

<http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html>

<code>
package news;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/** @author John B. Matthews */
public class GridTest {

private static final int ROWS = 5;
private static final int COLS = 4;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
//@ Override
public void run() {
new GridTest().create();
}
});
}

void create() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS * COLS; i++) {
panel.add(new LabelPanel());
}
JFrame f = new JFrame("GridTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

/**
* A LabelPanel has a label showing its current size & location.
*/
class LabelPanel extends JPanel {

private static final String FORE =
"<html><body align=center><b>";
private static final String AFT =
"</b><br><i>ubi in mundum</i></body></html>";
private final JLabel sizeLabel = new JLabel();
private final Point p = new Point();

public LabelPanel() {
this.setPreferredSize(new Dimension(150, 75));
this.add(sizeLabel);
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int w = e.getComponent().getWidth();
int h = e.getComponent().getHeight();
p.setLocation(0, 0);
SwingUtilities.convertPointToScreen(p, sizeLabel);
sizeLabel.setText(FORE
+ "Size: " + w + "\u00d7" + h + "<br>"
+ "Loc: " + p.x + "," + p.y
+ AFT);
}
});
}
}
</code>
 
K

Knute Johnson

Mark said:
I can get the current height and width of a window by commands like:

this.getWidth()
and
this.getHeight()

But how do I get current X and Y position of this window?
Mark

See Component.getBounds().
 
D

Daniel Pitts

Mark said:
I can get the current height and width of a window by commands like:

this.getWidth()
and
this.getHeight()

But how do I get current X and Y position of this window?
Mark
Look up getLocation()
 
I

Ian Shef

(e-mail address removed) (Mark Sizzler) wrote in @newsspool4.arcor-online.net:
I can get the current height and width of a window by commands like:

this.getWidth()
and
this.getHeight()

But how do I get current X and Y position of this window?
Mark

It would be helpful if you would be more specific about what you mean by
"window". If you mean java.awt.Component or any of its subclasses (e.g.
java.awt.Window, java.awt.Frame, javax.swing.JFrame, javax.swing.JWindow)
then the answer is

public Point getLocationOnScreen()

See the javadocs for Component for details.

Side note:
this.getWidth() and this.getHeight() can usually (always?) be simplified to
getWidth() and getheight().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,272
Latest member
MaricruzDu

Latest Threads

Top