This discussion is archived
10 Replies Latest reply: Jun 7, 2010 8:14 AM by 793415 RSS

How to change default background color of Panel?

843807 Newbie
Currently Being Moderated
In a paint function of the Panel I draw black background. But when I drag this panel I can see how this Panel is redrawing, and I can see flickering default system color of Panel (light color). I'd like to change this color not to see this flickering. (I'd like to change only default color of this panel so I can't use UIManager.put() function). How to get rid of this flickering?

ps. I have to use Panel, not JPanel.
  • 1. Re: How to change default background color of Panel?
    793415 Pro
    Currently Being Moderated
    For better help sooner, post an SSCCE.

    Also, why are you coding using AWT based components in this millennium?
  • 2. Re: How to change default background color of Panel?
    843807 Newbie
    Currently Being Moderated
    I need AWT Panel becouse this panel overlaps heavyweight component. Despite new JDK can handle mixing heavyweight and lightweight components, it doesn't work for me, I get heavyweight component above swing component even if swing component was added with lower index. Component hierarchy looks like this:

    JPanel1 --------------------------------> JPanel2 -------------------------------> Heavyweight Component
    .......................|
    .......................|--------------------> JPanel3 (*Panel)


    I'd like JPanel3 to appear above Heavyweight component if both components overlap. If I use Panel instead of JPanel everything is fine.

    Edited by: cartoon_20 on Jun 6, 2010 7:56 AM
    I added dots because it truncates leading white characters
  • 3. Re: How to change default background color of Panel?
    843807 Newbie
    Currently Being Moderated
    You can try setting the background color of the 2D graphic context associated with that object.
  • 4. Re: How to change default background color of Panel?
    843807 Newbie
    Currently Being Moderated
    use .setOpaque(false); which makes the panel colour not take over the colour of the frame
  • 5. Re: How to change default background color of Panel?
    793415 Pro
    Currently Being Moderated
    babojee wrote:
    use .setOpaque(false); ..
    (java.awt.)Panel has no setOpaque(boolean) method.
  • 6. Re: How to change default background color of Panel?
    843807 Newbie
    Currently Being Moderated
    morgalr wrote:
    You can try setting the background color of the 2D graphic context associated with that object.
    What do you mean? I paint a panel within paint funcion, and set a color with function setColor(Color c) of Graphics object.
  • 7. Re: How to change default background color of Panel?
    793415 Pro
    Currently Being Moderated
    BTW - some points about your reply to my first reply.
    1) What you posted is not an SSCCE.
    2) The problem with your ASCII art can be fixed by using the code tags. Please use the code tags for code or code snippets, HTML/XML, input/output or ASCII art. To use the code tags, select the sample and click the CODE button.
    3) Why did you ignore my question about using AWT?
  • 8. Re: How to change default background color of Panel?
    843807 Newbie
    Currently Being Moderated
    1) and 2) - ok I'll keep in mind this.
    3) I didn't ignore your question. I use AWT because I get ready heavyweight component (ready object, Canvas) . I can't even access paint function that component. I'd like to draw something above this Canvas, so I can't use Swing. Here is my original answer:
    "+Despite new JDK can handle mixing heavyweight and lightweight components, it doesn't work for me, I get heavyweight component above swing component even if swing component was added with lower index+."
  • 9. Re: How to change default background color of Panel?
    793415 Pro
    Currently Being Moderated
    cartoon_20 wrote:
    ..I didn't ignore your question. ..
    My bad. I did not read your reply carefully.
    ..I use AWT because I get ready heavyweight component (ready object, Canvas) . ..
    But this I just do not understand. A JPanel can do everything that a Canvas can do (though it might take a little extra coding). A JLayeredPane should be able to provide the layering you mention.
    ..I can't even access paint function that component. I'd like to draw something above this Canvas, so I can't use Swing. Here is my original answer:
    "+Despite new JDK can handle mixing heavyweight and lightweight components, it doesn't work for me, I get heavyweight component above swing component even if swing component was added with lower index+."
    Whichever way you decide to go, note that posting an SSCCE sure encourages people to help..
  • 10. Re: How to change default background color of Panel?
    843807 Newbie
    Currently Being Moderated
    Unless I am missing what you are saying, and I do that frequently, you are experiencing a flash of default color before you write your new color, if you change the background color of your graphics context to match your target color, then when you clear, you will have the color you desire already painted into the background. All you need do then is add your graphics.

    If that is not what you are trying to do, then explain further or just let me wanter aimlessly in the dark.

    Edited by: morgalr on Jun 8, 2010 8:36 PM ==> the following comment added:

    BTW: often when you get "flicker" it is a sign that you have a bloated paint/paintComponent method.