Link to home
Start Free TrialLog in
Avatar of Lescha
Lescha

asked on

Redrawing JFrame

I have an application written in JB3. At some point of execution, I call invalidate() method of the main window (which in this case extends JFrame) but it doesn't do anything. Why is that?

Also: I tried setBackground() method on the same main window. It works fine on Buttons and such, but on the JFrame it does the following: it changes the color for an instant (I can see the flicker) and then changes it back to light gray. Why is that?
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

The Component methods invalidate and validate are about ensuring that the component has a valid layout. They don't necessarily force the component to be redrawn. What you should do is also call repaint(). This will ensure that the component is redrawn. I've often found it necessary to call these methods (validate first) together.

As for JFrame background, while it is true that JFrame extends Component and so has a setBackground method, it is JFrame's content pane that you are actually viewing. The flicker is due to the fact that JFrame tries to draw its background and then redraws its children which causes its content pane to draw over. Call setBackground on the JFrame's content pane and that will fix this problem:
    frame.getContentPane().setBackground(Color.green);

Best regards,
Jim Cakalic
Avatar of Lescha
Lescha

ASKER

Thanks, please lock the question!
Jim, isn't the content pane transparent?
Lescha: you can click on the "Accept Comment as answer" on the title bar of Jim's comment.
Avatar of Lescha

ASKER

There is no such button on it. Don't know why, it is usually there.
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial