views:

442

answers:

2

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.

+3  A: 

Register a ComponentListener:

final Component myComponent = makeTheFrameOrWhatever();
myComponent.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e)
    {
        myComponent.repaint();
    }
});
Jonathan Feinberg
THANKYOU, WORKS!!
joef
@joef: Click on "accepted" answer if this was what you needed.
OscarRyz
I have a feeling I could wait until the world economy rebounds, and there will still be no "accept" on this one.
Jonathan Feinberg
A: 

You need to dig further because repaint() and paint() most certainly are called when you resize a frame - it's the only way the frame can cause it's contents to be painted. It is most likely that you're not seeing the repaint reach your specific component in the frame, perhaps because the particular layout you are using is not affected if the window is made larger. Or if you have no layout, then you must subclass Frame and explicitly have it call paint on the subcomponents, since there is no layout manager to do that for you.

Note that whether or not painting is done repeatedly while you are resizing, as opposed to just once after you let go the mouse button may be an operating system option.

Software Monkey