Scott Odle
Scott Odle

Reputation: 290

JPanel.getBounds not updating

I have a simple app with a JPanel, and a Timer that's meant to print the size and position of the panel with every frame.

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class GetBoundsTest extends JPanel {

    public GetBoundsTest() {

        setSize(100, 100);

        Timer t = new Timer(40, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(GetBoundsTest.this.getBounds());
            }
        });

        t.start();
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.add(new GetBoundsTest());

        f.setVisible(true);

        f.setBounds(new Rectangle(50, 50, 50, 50));
    }

}   

The problem I have is that the bounds don't update when the window is moved.

Upvotes: 0

Views: 391

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

The bounds of the panel won't change (while the frame size doesn't change) as the panels position is relative to the frame.

Try looking at the bounds of the frame instead of the panel

Upvotes: 3

Related Questions