0

I am not asking how to use Java swing nor I am asking for suggestion on using layout managers. I am just curious how Java behaves.

All along it has been a myth and many people speculate that Java automatically repaints the components when you resize the frame OR mouse over the components in the frame.

So my question is: Is it true that Java does the repainting automatically when we carry out one of the above actions?


There has been several post with similar title such as: Java repainting a component at mouse-over.

But no one can give a definite answer whether Java does the repainting automatically upon certain user actions (such as resizing & mouse over).

Community
  • 1
  • 1
user3437460
  • 17,253
  • 15
  • 58
  • 106

1 Answers1

4

All along it has been a myth and many people speculate

There is no myth or speculation.

automatically repaints the components when you resize

This makes sense because the layout manager is invoked and the size or location may change which means some components may need to be repainted.

automatically repaints the components when you mouse over the components in the frame.

It depends on the component. If a MouseListener has been added to the component to do special processing (ie. rollover a button) then the component may be repainted, otherwise nothing happens. But there is no default painting unless it has been specifically added as part of the UI for the component.

These question is easily verified. Just override the paintCompent() method of your components to display a message when the component is painted and see what happens.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I override the repaint() method and paintComponent() method with some println statements, but it doesn't show anything even when I resize the frame. Are you sure the `app-triggered` painting is using paintComponent() and not other methods? – user3437460 Feb 15 '15 at 19:43
  • 2
    Yes. Did you read the link provided in the initial comment to your question? You can also read the section from the Swing tutorial on [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html). Post your [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Feb 15 '15 at 20:44