Get position of component : Swing Introduction « Swing « Java Tutorial






import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

public class Main extends JFrame {
  JComponent comp = new JLabel("Test label");

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    comp.setToolTipText("Some tooltip text for component");
    comp.setBorder(new TitledBorder("Button"));

    System.out.println("X:" + comp.getX() + " Y:" + comp.getY() + " width:"
        + comp.getWidth() + " height:" + comp.getHeight());

    getContentPane().add(comp);
    pack();

    System.out.println("X:" + comp.getX() + " Y:" + comp.getY() + " width:"
        + comp.getWidth() + " height:" + comp.getHeight());

    setVisible(true);
  }

  public static void main(String arg[]) {
    new Main();
  }
}








14.1.Swing Introduction
14.1.1.Swing MVC
14.1.2.Swing components
14.1.3.Understanding the Predefined Data Models: Swing Component Models
14.1.4.Small Swing ApplicationSmall Swing Application
14.1.5.Swing Constants
14.1.6.Swing Worker from JDK 6 SE
14.1.7.Get position of component