Repaint frame when resized?

11,700

Solution 1

Register a ComponentListener:

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

Solution 2

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.

Share:
11,700

Related videos on Youtube

joef
Author by

joef

Updated on April 21, 2022

Comments

  • joef
    joef about 2 years

    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.

  • OscarRyz
    OscarRyz over 14 years
    @joef: Click on "accepted" answer if this was what you needed.
  • Jonathan Feinberg
    Jonathan Feinberg over 14 years
    I have a feeling I could wait until the world economy rebounds, and there will still be no "accept" on this one.
  • gdbj
    gdbj about 7 years
    Safe to say the economy has come back by now. Your intuition is good!