I am a complete newcomer to Java, so apologies for this basic question. I have a JPanel sitting inside of a JFrame. I call the panel's getY() method, and expect the result to give me the y-coordinate of the panel's top edge in the frame's coordinate system. But it isn't even close. The below example demonstrates what I'm encountering. First it prints (after "XXX") the panel's y-coordinate according to getY() (and its border thickness, for good measure). Then, whenever the mouse moves, it prints the (x,y) coords of the mouse pointer in the frame's coordinate system.
According the output printed after XXX, the panel's y-coordinate is 58 and its border thickness if 4. But moving the mouse around on my screen, I'm still well inside the button panel at y-coordinate 58. The frame's border seems to start around y-coordinate 85 or so.
I assume this has something to do with the fact that I'm using a layout manager, but perhaps I'm mistaken. In any event, how do I get the correct y-coordinate for the top of the panel in question?
Thanks in advance for help!
import java.awt.BorderLayout; import java.awt.Container; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.BoxLayout; import javax.swing.border.Border; import javax.swing.BorderFactory; public class Example extends JFrame implements MouseMotionListener { public Example() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Get Pane and set layout Container pane = getContentPane(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); // Add Buttons JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); pane.add(buttonPanel); JButton button1 = new JButton("Button1"); button1.setAlignmentX(Component.CENTER_ALIGNMENT); buttonPanel.add(button1); JButton button2 = new JButton("Button2"); button2.setAlignmentX(Component.CENTER_ALIGNMENT); buttonPanel.add(button2); // Set up the drawing area JPanel drawingPanel = new JPanel(); drawingPanel.setAlignmentY(Component.BOTTOM_ALIGNMENT); Border raisedBorder = BorderFactory.createRaisedBevelBorder(); Border loweredBorder = BorderFactory.createLoweredBevelBorder(); Border compoundBorder = BorderFactory.createCompoundBorder(raisedBorder, loweredBorder); drawingPanel.setBorder(compoundBorder); drawingPanel.setPreferredSize(new Dimension(1000,1000)); drawingPanel.setMaximumSize(new Dimension(1000,1000)); pane.add(drawingPanel); // Set up the main window pane.setPreferredSize(new Dimension(1050,1100)); addMouseMotionListener(this); pack(); setVisible(true); System.out.printf("XXX: %d, %d\n", drawingPanel.getY(),compoundBorder.getBorderInsets(drawingPanel).top); } public void paint(Graphics g) { paintComponents(g); } public void mouseMoved(MouseEvent me) { int x = me.getX(); int y = me.getY(); System.out.printf("(%d, %d)\n", x, y); } public void mouseDragged(MouseEvent me) { } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); Example frame = new Example(); } }