Repaint frame when resized?

joef picture joef · Oct 15, 2009 · Viewed 11.5k times · Source

How would I force a Frame to repaint() immediately after it is maximized, or resized?

I can't find what method is called when that specific operation takes place. I have a bunch of graphics that are written with Graphic objects in paint and their orientation depends on the real time feedback from getWidth() and getHeight() but paint isn't called when I maximize, only when those pixels change unfortunately.

Answer

Jonathan Feinberg picture Jonathan Feinberg · Oct 15, 2009

Register a ComponentListener:

final Component myComponent = makeTheFrameOrWhatever();
myComponent.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e)
    {
        myComponent.repaint();
    }
});